首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >部署ASP.NET MVC4Web API

部署ASP.NET MVC4Web API
EN

Stack Overflow用户
提问于 2012-03-14 00:33:43
回答 2查看 5.2K关注 0票数 2

部署ASP.NET MVC4Web API时出现问题

我尝试部署一个ASP.NET MVC4Web API站点。一切正常,但是如果我从外部调用服务器并返回HTTP状态400或更高,Web服务器将接管响应。

在服务器上本地调用可以很好地工作。

示例:

代码语言:javascript
运行
复制
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。

代码语言:javascript
运行
复制
// 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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2012-03-14 16:43:02

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

代码语言:javascript
运行
复制
Response.TrySkipIisCustomErrors = true;

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

票数 6
EN

Stack Overflow用户

发布于 2014-03-19 02:22:31

不是返回新的HttpResponseMessage,而是使用请求,这样响应就是完全水合的,并将通过IIS原封不动地返回。

代码语言:javascript
运行
复制
return Request.CreateResponse(HttpStatusCode.OK, "Status Code : " + StatusCode.ToString());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9688118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档