如何在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" /> 

发生异常时,我仍然看到黄色画面。我做错了什么?

我会想象,调用base.OnException处理程序是什么导致你的问题。在没有真正查看代码的情况下,我会想象它负责处理错误并生成异常和堆栈跟踪的响应。从代码中移除该行 - 不需要,因为您无论如何都要替换ViewResult。

我建议您使用ELMAH并实现一个HandleError属性,它可以与它一起工作:请参阅此question。 ELMAH非常灵活和配置驱动,而不是代码驱动。

+0

我试过注释掉base.OnException行,并且我得到了同样的黄页。 – Picflight 2010-06-17 06:42:13

+0

@Picflight - 启动调试器并查看发生了什么的时间。它可能会从您的代码中获得早期回报。你的代码本身也可能导致异常 - 例如,我们不知道'MailException'内发生了什么。我会在该方法的顶部放置一个断点并逐步查看发生了什么。 – tvanfosson 2010-06-17 12:09:53

Server.ClearError() 

如果您打电话给我,会怎么样?