2013-05-16 15 views
12

該主題是自選的。我有開發人員和生產環境。開發人員env。是我的本地主機。 contolers中的action方法在響應錯誤(錯誤發生或邏輯不一致)並返回Json-answer時將響應狀態碼設置爲500。我的常見方法如下所示:使用HTML而不是使用ASP上的JSON的IIS響應MVC3

[HttpPost] 
public ActionResult DoSomething(int id) 
{ 
    try 
    { 
     // some useful code 
    } 
    catch(Exception ex) 
    { 
     Response.StatusCode = 500; 
     Json(new { message = "error" }, JsonBehaviour.AllowGet) 
    } 
} 

在客戶端生產env。當發生這樣的錯誤時,ajax.response看起來像一個HTML代碼,而不是預期的JSON。

考慮一下:

<div class="content-container"> 
<fieldset> 
    <h2>500 - Internal server error.</h2> 
    <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3> 
</fieldset> 
</div> 

過濾方面是不是一種選擇。我認爲這是某種IIS或web.config問題。

SOLUTION: 我們決定在Global.asax中添加TrySkipIisCustomErrors中的BeginRequest和它解決了我們的應用程序的每個方法的問題。

+1

你的'Json'函數調用之前有'return'? – Zoka

+0

@Zoka,是的corse **返回**仍然存在。謝謝。 – kokosda

回答

12

我想IIS正在提供一些友好的錯誤頁面。您可以嘗試通過設置在響應TrySkipIisCustomErrors屬性跳過此頁:

catch(Exception ex) 
{ 
    Response.StatusCode = 500; 
    Response.TrySkipIisCustomErrors = true; 
    return Json(new { message = "error" }, JsonBehaviour.AllowGet) 
} 
+0

@Khanh To,跳過iis錯誤似乎是某種破解 – kokosda

+1

不,完全不是。 hack。友好的錯誤頁面是ASP.NET中的一個功能,如果你不喜歡它們,就不要使用它們,但是如果你決定使用它們,你應該知道它的後果(你不能設置錯誤狀態代碼 –

+0

我認爲這是一個很好的解決方案,但似乎仍然是一個IIS問題,我們決定在Global.asax的BeginRequest中添加'TrySkipIisCustomErrors',它解決了我們每個方法中的問題應用程序,謝謝 – kokosda

0

是否將您的IIS配置爲將application/json視爲有效的mime-type?您可以在IIS管理器中檢查服務器的屬性,然後單擊MIME類型。如果json不在那裏,那麼點擊「新建」,爲擴展輸入「JSON」,爲MIME類型輸入「application/json」。

+0

nope :(這個類型實際上沒有在我的iis上註冊,我添加了它,但是沒有任何變化 – kokosda

0

我通過編寫自定義JSON結果,它採用json.net作爲串行解決了這個。這只是IIS修復的矯枉過正,但它意味着它可以重用。

public class JsonNetResult : JsonResult 
{ 
    //public Encoding ContentEncoding { get; set; } 
    //public string ContentType { get; set; } 
    public object Response { get; set; } 
    public HttpStatusCode HttpStatusCode { get; set; } 

    public JsonSerializerSettings SerializerSettings { get; set; } 
    public Formatting Formatting { get; set; } 

    public JsonNetResult(HttpStatusCode httpStatusCode = HttpStatusCode.OK) 
    { 
     Formatting = Formatting.Indented; 
     SerializerSettings = new JsonSerializerSettings { }; 
     SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 
     HttpStatusCode = httpStatusCode; 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
      throw new ArgumentNullException("context"); 

     HttpResponseBase response = context.HttpContext.Response; 

     response.TrySkipIisCustomErrors = true; 

     response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; 

     if (ContentEncoding != null) 
      response.ContentEncoding = ContentEncoding; 

     response.StatusCode = (int) HttpStatusCode; 

     if (Response != null) 
     { 
      JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; 

      JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); 
      serializer.Serialize(writer, Response); 

      writer.Flush(); 
     } 
    } 
} 

用途:

  try 
      { 
       return new JsonNetResult() 
       { 
        Response = "response data here" 
       }; 
      } 
      catch (Exception ex) 
      { 
       return new JsonNetResult(HttpStatusCode.InternalServerError) 
       { 
        Response = new JsonResponseModel 
        { 
         Messages = new List<string> { ex.Message }, 
         Success = false, 
        } 
       }; 
      } 
相關問題