部署ASP.NET MVC 4 Web API

Joa*_*kim 2 asp.net asp.net-mvc-4 asp.net-web-api

部署ASP.NET MVC 4 Web API时出现问题

我尝试部署ASP.NET MVC 4 Web API站点.一切正常但如果我从外部调用服务器并返回400及以上的HTTP状态,Web服务器将接管响应.

在服务器上本地调用工作正常.

例:

Work fine:
 http:// myTestServer.se/Test/api/Test/200 
 http:// myTestServer.se/Test/api/Test/399 

Does not work, the Web server takes over the response: 
 http:// myTestServer.se/Test/api/Test/400 
 http:// myTestServer.se/Test/api/Test/599 
      For those cases where there is an Error Pages is returned. For an invalid code is returned ”The custom error module does not recognize this error.”

if I make the calls locally on the server, it works fine:
 http://localhost/Test/api/Test/400
 http://localhost/Test/api/Test/599
Run Code Online (Sandbox Code Playgroud)

我的简单测试代码会将收到的ID作为HTTP状态返回.

// GET /api/values/200
public HttpResponseMessage<string> Get(int id)
{
    try
    {
        HttpStatusCode StatusCode = (HttpStatusCode)id;
        return new HttpResponseMessage<string>("Status Code : " + StatusCode.ToString(), StatusCode);
    }
    catch {
        return new HttpResponseMessage<string>("Unable to convert the id", HttpStatusCode.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

tpe*_*zek 5

这就是所谓的"智能错误消息"问题.IIS正在劫持您的错误消息并将其替换为自己的错误消息.典型的解决方法是将TrySkipIisCustomErrors设置为true:

Response.TrySkipIisCustomErrors = true;
Run Code Online (Sandbox Code Playgroud)

我没有检查这是否适用于Web API.您可以在此处此处阅读有关此问题的更多信息.

  • 通过根据[asp-net-web-api-httpresponseexception-400-bad-request-hijacked-by-iis]中的响应配置Web服务器解决了这个问题(http://stackoverflow.com/questions/9985327/asp-净网页API-httpresponseexception-400-坏请求劫持逐IIS) (2认同)