c# – ASP.NET Web API应用程序为启用了TransferRequestHandler的非存在路由返回HTTP 500

我创建了一个简单的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" });
    }
}

现在我们需要在路径变量中支持文件扩展名(例如file.pdf),所以我修改了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>

现在的问题是HTTP状态代码是不一致的,这取决于前缀/ api / test /之后提供的URL段:

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)

500错误页面以HTML格式显示,应用程序日志中没有记录任何内容,不会抛出任何异常.我使用VS 2015,使用.NET framework 4.5.1.

最佳答案
我也看到了这一点 – 我看到我的global.asax.cs“BeginRequest”和“EndRequest”大约被调用了10次这样的请求.看起来像一个ASP.NET / WebApi错误.

我找到的解决方案是使用始终返回404的控制器注册“catch all”路由.

        config.Routes.MapHttpRoute(
            name: "CatchAllRoute",
            routeTemplate: "{*catchall}",
            defaults: new { controller = "UnrecognizedRoute", action = ApiConstants.UnrecognizedRouteAction });

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();
    }
}

转载注明原文:c# – ASP.NET Web API应用程序为启用了TransferRequestHandler的非存在路由返回HTTP 500 - 代码日志