部署ASP.NET MVC 4 Web API

部署ASP.NET MVC 4 Web API,asp.net,asp.net-mvc-4,asp.net-web-api,Asp.net,Asp.net Mvc 4,Asp.net Web Api,部署ASP.NET MVC 4 Web API时出现问题 我尝试部署一个ASP.NETMVC4WebAPI站点。一切正常,但如果我从外部调用服务器并返回HTTP状态400及以上,Web服务器将接管响应 在服务器上本地调用可以正常工作 例如: Work fine: http:// myTestServer.se/Test/api/Test/200 http:// myTestServer.se/Test/api/Test/399 Does not work, the Web server

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

我尝试部署一个ASP.NETMVC4WebAPI站点。一切正常,但如果我从外部调用服务器并返回HTTP状态400及以上,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
我的简单测试代码将以HTTP状态返回收到的ID

// 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);
    }
}
//GET/api/values/200
公共HttpResponseMessage获取(int id)
{
尝试
{
HttpStatusCode StatusCode=(HttpStatusCode)id;
返回新的HttpResponseMessage(“状态代码:+StatusCode.ToString(),StatusCode”);
}
抓住{
返回新的HttpResponseMessage(“无法转换id”,HttpStatusCode.OK);
}
}
这就是所谓的“智能错误消息”问题。IIS正在劫持您的错误消息并将其替换为自己的错误消息。典型的解决方法是将tryskipiiscustomerrs设置为true:

Response.TrySkipIisCustomErrors = true;
我还没有检查这是否适用于Web API。您可以阅读有关此问题的更多信息。

使用请求,而不是返回新的HttpResponseMessage,以使响应完全水合,并使其通过IIS恢复原状

return Request.CreateResponse(HttpStatusCode.OK, "Status Code : " + StatusCode.ToString());

通过根据Joakim的apprach中的响应配置Web服务器解决了这个问题,但这对我来说不起作用。我需要在这里使用这种方法:为了能够让您的web服务器和本地服务器执行相同的操作,请将IIS errorMode设置设置为Custom,而不是DetailedLocalOnly(这是默认设置)。我注意到我必须指定第二个参数(内容),否则IIS仍将显示自定义错误页面。