2011-12-01 59 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