該主題是自選的。我有開發人員和生產環境。開發人員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和它解決了我們的應用程序的每個方法的問題。
你的'Json'函數調用之前有'return'? – Zoka
@Zoka,是的corse **返回**仍然存在。謝謝。 – kokosda