我有一个看起来像这样的自定义错误控制器:
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 来测试我的 500 处理;我被重定向到errors/error500(很好)。但是,异常数据不会呈现在页面上,因为 Server.GetLastError() 调用返回 null 而不是 RaiseError() 引发的异常。
处理自定义 500 错误页面的最佳方法是什么,该自定义页面也可以呈现异常详细信息?