Max*_*ini 6 c# asp.net iis asp.net-web-api
我创建了一个简单的Web API应用程序(Visual Studio中的空模板,启用了Web API),添加了一个控制器:
[RoutePrefix("api/test")]
public class TestController : ApiController
{
[HttpGet]
[Route(@"resource/{*path?}")]
public async Task<HttpResponseMessage> GetFolder(string path = "")
{
return this.Request.CreateResponse(HttpStatusCode.OK, new { Status = "OK" });
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们需要file.pdf在path变量中支持文件扩展名(例如),所以我修改了web.config:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<!-- API must handle all file names -->
<add name="ApiUrlHandler" path="/api/test/*" verb="GET,POST,PUT,DELETE,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
现在的问题是HTTP状态代码是不一致的,具体取决于前缀后提供的URL段/api/test/:
GET /api/test/resource => HTTP 200 (as expected)
GET /api/test/resource/foo => HTTP 200 (as expected)
GET /api/test/foo => HTTP 404 (as expected)
GET /api/test/foo/bar => HTTP 500 (expected: 404)
Run Code Online (Sandbox Code Playgroud)
500错误页面以HTML格式显示,应用程序日志中没有记录任何内容,不会抛出任何异常.我使用VS 2015,使用.NET framework 4.5.1.
我也看到了这一点 - 我发现我的 global.asax.cs“BeginRequest”和“EndRequest”对于此类请求被调用了大约 10 次。看起来像是 ASP.NET/WebApi 错误。
我找到的解决方案是使用始终返回 404 的控制器注册“捕获所有”路由。
config.Routes.MapHttpRoute(
name: "CatchAllRoute",
routeTemplate: "{*catchall}",
defaults: new { controller = "UnrecognizedRoute", action = ApiConstants.UnrecognizedRouteAction });
Run Code Online (Sandbox Code Playgroud)
...
public class UnrecognizedRouteController : ApiController
{
/// <summary>
/// This method is called for every single API request that comes to the API and is not routed
/// </summary>
[ActionName(ApiConstants.UnrecognizedRouteAction)]
[HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPost, HttpPatch, HttpPut]
public IHttpActionResult ProcessUnrecognizedRoute()
{
return NotFound();
}
}
Run Code Online (Sandbox Code Playgroud)