如何在Asp.Net MVC显示一个通用的错误页面2
问题描述:
我已经在我的基本控制器如下:如何在Asp.Net MVC显示一个通用的错误页面2
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
// If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
Exception exception = filterContext.Exception;
// If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != 500)
{
return;
}
// TODO: What is the namespace for ExceptionType?
//if (!ExceptionType.IsInstanceOfType(exception))
//{
// return;
//}
// Send Email
MailException(exception);
// TODO: What does this line do?
base.OnException(filterContext);
filterContext.Result = new ViewResult
{
ViewName = "Error"
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
}
在我的共享文件夹,我有一个Error.aspx视图。
的Web.config
<customErrors mode="On" />
发生异常时,我仍然看到黄色画面。我做错了什么?
答
Server.ClearError()
如果您打电话给我,会怎么样?
我试过注释掉base.OnException行,并且我得到了同样的黄页。 – Picflight 2010-06-17 06:42:13
@Picflight - 启动调试器并查看发生了什么的时间。它可能会从您的代码中获得早期回报。你的代码本身也可能导致异常 - 例如,我们不知道'MailException'内发生了什么。我会在该方法的顶部放置一个断点并逐步查看发生了什么。 – tvanfosson 2010-06-17 12:09:53