我是 ASP.NET MVC3 的新手。如何为 MVC3 创建全局自定义错误页面?一般的想法是,当抛出异常时,它会向用户显示一条通用消息,并将异常记录到数据库中,供开发人员稍后进行调查。
在此先感谢您的帮助。
这是我最终在 global.asax.cs 中所做的事情:
protected void Application_Error()
{
var exception = Server.GetLastError();
Log.Error("Exception", exception);
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorsController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}