4
我有一個自定義錯誤控制器,它看起來像這樣:顯示自定義錯誤頁,並顯示異常細節
public class ErrorsController : BaseController
{
public ActionResult RaiseError(string error = null)
{
string msg = error ?? "An error has been thrown (intentionally).";
throw new Exception(msg);
}
public ActionResult Error404()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
public ActionResult Error500()
{
Response.TrySkipIisCustomErrors = true;
var model = new Models.Errors.Error500()
{
ServerException = Server.GetLastError(),
HTTPStatusCode = Response.StatusCode
};
return View(model);
}
}
我Errors500.cshtml看起來是這樣的:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Error500</title>
</head>
<body>
<div>
An internal error has occurred.
@if (Model != null && Model.ServerException != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.ServerException.Message<br />
</p>
<div style="overflow:scroll">
<pre>
@Model.ServerException.StackTrace
</pre>
</div>
</div>
}
</div>
</body>
</html>
和我的web.config有我的錯誤處理程序指定爲:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace" >
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" responseMode="ExecuteURL" path="/Errors/Error404" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" subStatusCode="-1" responseMode="ExecuteURL" path="/Errors/Error500" />
</httpErrors>
問題是:每當我調用/ errors/raiseerror to tes我的500處理;我被重定向到錯誤/ error500(罰款)。但是,由於Server.GetLastError()調用返回null而不是由RaiseError()引發的異常,因此不會在頁面上呈現異常數據。
處理自定義500錯誤頁面的最佳方式是什麼,該自定義頁面可以渲染出異常詳細信息?