部署ASP.NET MVC4Web API时出现问题
我尝试部署一个ASP.NET MVC4Web API站点。一切正常,但是如果我从外部调用服务器并返回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);
}
}
发布于 2012-03-14 16:43:02
这就是所谓的“智能错误消息”问题。IIS劫持了您的错误消息,并将其替换为自己的错误消息。通常的解决方法是将TrySkipIisCustomErrors设置为true:
Response.TrySkipIisCustomErrors = true;
我还没有检查这是否适用于Web API。您可以阅读有关问题here和here的更多信息。
发布于 2014-03-19 02:22:31
不是返回新的HttpResponseMessage,而是使用请求,这样响应就是完全水合的,并将通过IIS原封不动地返回。
return Request.CreateResponse(HttpStatusCode.OK, "Status Code : " + StatusCode.ToString());
https://stackoverflow.com/questions/9688118
复制相似问题