无法远程返回自定义HTTP错误详细信息

时间:2011-12-01 19:04:07

标签: c# asp.net-mvc iis

这是一个奇怪的。我正在运行MVC 3并且具有自定义操作结果,该结果包装异常并返回消息以及标准HTTP错误。

public class ExceptionResult : ActionResult
{
    private readonly Exception _exception;

    public ExceptionResult(Exception exception)
    {
        _exception = exception;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ClearHeaders();
        response.Cache.SetNoStore();
        response.ContentType = ContentType.Json;

        var baseEx = _exception as BaseException ?? new ServerException(_exception);

        var result = baseEx.GetResult();

        var json = result.ToJSON();
        response.Write(json);
        response.StatusCode = (int)result.Status.Code;
    }
}

当我在本地运行时,我得到了我期望的结果:

HTTP/1.1 400 Bad Request
Cache-Control: no-store
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Date: Thu, 01 Dec 2011 19:00:03 GMT
Content-Length: 81

{"error":"invalid_request","error_description":"Parameter grant_type is missing"}

但是当我尝试从另一台机器连接时,我得到了标准的IIS错误消息:

HTTP/1.1 400 Bad Request
Cache-Control: no-store
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Date: Thu, 01 Dec 2011 19:02:33 GMT
Content-Length: 11

Bad Request

更新

IIS管道中某处必须有一些http模块,它会吞下响应并重写内容。我写了一个模块来记录请求和响应,并且它正好返回了我所期望的但实际上它对浏览器的影响是错误的。

2011-12-02 15:39:00,518 - ======== Request ========
2011-12-02 15:39:00,518 - GET /oauth/2/token HTTP/1.1
2011-12-02 15:39:00,519 - Cache-Control: max-age=0
2011-12-02 15:39:00,519 - Connection: keep-alive
2011-12-02 15:39:00,519 - Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
2011-12-02 15:39:00,519 - Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
2011-12-02 15:39:00,519 - Accept-Encoding: gzip,deflate,sdch
2011-12-02 15:39:00,519 - Accept-Language: en-US,en;q=0.8
2011-12-02 15:39:00,519 - Host: micah-pc:8095
2011-12-02 15:39:00,519 - User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
2011-12-02 15:39:00,519 - =========================
2011-12-02 15:39:00,519 - OAuth exception occurred.
BoomTown.OAuth.OAuthException: Parameter grant_type is missing
   at BoomTown.OAuth.Request.TokenRequest.GetRequestValidator() in C:\code\BoomTown\Api\BoomTown.OAuth\Request\TokenRequest.cs:line 19
   at BoomTown.OAuth.Request.OAuthRequestBase.Validate() in C:\code\BoomTown\Api\BoomTown.OAuth\Request\OAuthRequestBase.cs:line 33
   at BoomTown.OAuth.Request.OAuthRequestBase..ctor(HttpRequestBase request, IOAuthServiceLocator serviceLocator) in C:\code\BoomTown\Api\BoomTown.OAuth\Request\OAuthRequestBase.cs:line 28
   at BoomTown.OAuth.Request.TokenRequest..ctor(HttpRequestBase request, IOAuthServiceLocator serviceLocator) in C:\code\BoomTown\Api\BoomTown.OAuth\Request\TokenRequest.cs:line 13
   at BoomTown.Api.Web.Controllers.OAuth.V2.OAuthController.Token() in C:\code\BoomTown\Api\BoomTown.Api.Web\Controllers\OAuth\V2\OAuthController.cs:line 26
2011-12-02 15:39:00,520 - ======= Response =======
2011-12-02 15:39:00,520 - HTTP/1.1 400 Bad Request
2011-12-02 15:39:00,520 - Cache-Control: no-store
2011-12-02 15:39:00,520 - X-AspNet-Version: 4.0.30319
2011-12-02 15:39:00,520 - Content-Type: application/json; charset=utf-8
2011-12-02 15:39:00,520 - {"error":"invalid_request","error_description":"Parameter grant_type is missing"}

由于一点点的调查,我能够弄明白。我设置了IIS跟踪,这证实了我怀疑它与 customerrormodule 有关,它正在拦截我的请求并覆盖我的错误消息。

我一直在跟你打招呼
<system.web>
  <customErrors />
<system.web>

设置但无济于事。我是在正确的轨道上,但由于我正在运行它的IIS 7,我需要更改正确的web.config部分,如下所示:

  <system.webServer>
    <httpErrors errorMode="Detailed" />
  </system.webServer>

现在我所有的自定义JSON消息都完美无缺。非常感谢Jason Finneyfrock对此标记团队的支持。

2 个答案:

答案 0 :(得分:9)

在您的web.config中,您是否将httpErrors定义为DetailedLocalOnly?我不确定在这种情况下是否会删除内容。

http://www.iis.net/ConfigReference/system.webServer/httpErrors

答案 1 :(得分:3)

我遇到过这个,不确定它是否会有所帮助:

context.HttpContext.Response.TrySkipIisCustomErrors = true;