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

时间:2020-12-10 00:18:38

This is a strange one. I'm running MVC 3 and have a custom action result that wraps exceptions and returns a message along with the standard HTTP error.

这是一个奇怪的。我正在运行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;
    }
}

When I run this locally I get exactly what I expect:

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

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"}

But when I try to connect from a different machine I get the standard IIS error message instead:

但是当我尝试从另一台机器连接时,我得到了标准的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

UPDATE

UPDATE

There must be some http module somewhere in the IIS pipeline that is swallowing the response and rewriting the content. I wrote a module to log the request and response and it's returning exactly what I expect however what actually makes it to the browser is wrong.

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"}

SOLUTION

Thanks to a little sleuthing I was able to figure it out. I setup IIS tracing which confirmed my suspicions that it was related to the customerrormodule which was intercepting my requests and overwriting my error messages. I kept monkeying with the

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

<system.web>
  <customErrors />
<system.web>

settings but to no avail. I was on the right track, but since it's IIS 7 that I'm running I needed to change the correct web.config section like this:

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

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

Now all my custom JSON messages come through perfectly. Big thanks to Jason Finneyfrock for the tag team on this one.

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

2 个解决方案

#1


9  

In your web.config, do you have httpErrors defined to only be DetailedLocalOnly? I'm not sure whether or not the content would be removed in this situation.

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

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

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

#2


3  

I came across this, not sure if it will help:

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

context.HttpContext.Response.TrySkipIisCustomErrors = true;

#1


9  

In your web.config, do you have httpErrors defined to only be DetailedLocalOnly? I'm not sure whether or not the content would be removed in this situation.

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

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

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

#2


3  

I came across this, not sure if it will help:

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

context.HttpContext.Response.TrySkipIisCustomErrors = true;