2011-12-01 89 views
16

这是一个奇怪的问题。我正在运行MVC 3,并有一个自定义操作结果,它包装异常并返回一条消息以及标准的HTTP错误。不能远程返回自定义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 

UPDATE

必须有这样的我的http模块在IIS管道的某个地方吞咽响应并重写内容。我编写了一个模块来记录请求和响应,并且它完全返回了我期望的内容,但实际上它对浏览器的影响是错误的。

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

多亏了小侦探我能弄明白。我设置了IIS跟踪,证实了我的怀疑,它与customerrormodule有关,它拦截了我的请求并覆盖了我的错误消息。我一直揣着

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

设置,但无济于事。我是在正确的轨道上,但因为它是IIS 7中我跑我需要改变正确的web.config部分,像这样:

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

现在我所有的自定义JSON消息来通过完美。非常感谢Jason Finneyfrock这个标签团队。

+0

+1好问题,你确定你是返回结果或IIS拦截?我注意到Content-Type在远程响应上是不同的。 –

回答

3

我碰到这个,不知道这是否会帮助:

context.HttpContext.Response.TrySkipIisCustomErrors = true; 
+0

我刚试过,它什么都没做。 – Micah