asp.net - 部署 ASP.NET MVC 4 Web API

标签 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

我的简单测试代码会将收到的 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);
    }
}

最佳答案

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

Response.TrySkipIisCustomErrors = true;

我还没有检查这是否适用于 Web API。您可以阅读有关该问题的更多信息 herehere .

关于asp.net - 部署 ASP.NET MVC 4 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688118/

相关文章:

asp.net - jVectorMap - 如何获取美国各州的县 map ?

concurrency - 并发使用MVC应用的Stack Empty问题

c# - 与 WebApi 重叠的路由

c# - 确保 WebAPI2 路由在 MVC 路由之前匹配

angularjs - 如何将 AngularJS $http.post 中的多个参数发送到 Web API Controller 操作方法

Asp.Net MVC 时间跨度

c# SELECT 与 FOR JSON

c# - C#/WSC (COM) 互操作中的 FatalExecutionEngineError

c# - 通过将对象列表附加到 jquery formdata 中来传递对象列表

asp.net-mvc-4 - 多个 Entity Framework 迁移