ASP.NET MVC中的统一化自定义异常处理

使用系统默认的错误处理方式,可以减少一次重定向访问,并且正常提示了错误信息,并发送了错误码500,但应注意以下问题:

一、需要修改web.config

    <customErrors mode="On">
    </customErrors>

二、如果修改默认的Error.cshtml文件,不使用默认布局页或修改布局页,应引用下列文件

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")

三、默认的HandleErrorAttribute只处理500错误,所以应重写此类并加入过滤器。写入日志。

HandleErrorAttribute源码(可能与最新源码不一致)

 

        public virtual void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (filterContext.IsChildAction)
            {
                return;
            }

            // 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;
            }

            if (!ExceptionType.IsInstanceOfType(exception))
            {
                return;
            }

            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
            filterContext.Result = new ViewResult
            {
                ViewName = View,
                MasterName = Master,
                ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                TempData = filterContext.Controller.TempData
            };
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;

            // Certain versions of IIS will sometimes use their own error page when
            // they detect a server error. Setting this property indicates that we
            // want it to try to render ASP.NET MVC's error page instead.
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }

 

 

如果不使用过滤器,参考下面内容

 

===========================================================================================================================================

使用HandleErrorAttribute后,找到了Error.cshtml,则此时异常已经被捕获处理,所以不会再次被Application_Error捕获处理。

所以啊,如果想使用Application_Error,就要

 

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
        }
    }

 

 

 

 

 

 

=======================================================================================================================================================================================

当ASP.NET MVC程序出现了异常,怎么处理更加规范?

1. 最简单的方法是设置<customErrors/>节点

<customErrors>节点用于定义一些自定义错误信息的信息。此节点有Mode和defaultRedirect两个属性,其中defaultRedirect属性是一个可选属性,表示应用程序发生错误时重定向到的默认URL,如果没有指定该属性则显示一般性错误。Mode属性是一个必选属性,它有三个可能值,它们所代表的意义分别如下: On 表示在本地和远程用户都会看到自定义错误信息。 Off 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息。 RemoteOnly 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。

这里有必要说明一下本地用户和远程用户的概念。当我们访问asp.net应用程时所使用的机器和发布asp.net应用程序所使用的机器为同一台机器时成为本地用户,反之则称之为远程用户。在开发调试阶段为了便于查找错误Mode属性建议设置为Off,而在部署阶段应将Mode属性设置为On或者RemoteOnly,以避免这些详细的错误信息暴露了程序代码细节从而引来黑客的入侵。

<error>子节点

在<customErrors>节点下还包含有<error>子节点,这个节点主要是根据服务器的HTTP错误状态代码而重定向到我们自定义的错误页面,注意要使<error>子节点下的配置生效,必须将<customErrors>节点节点的Mode属性设置为“On”。下面是一个例子:

  1. <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">   
  2.      <error statusCode="403" redirect="403.htm" />   
  3.      <error statusCode="404" redirect="404.htm" />   
  4. </customErrors>  
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm"> 
     <error statusCode="403" redirect="403.htm" /> 
     <error statusCode="404" redirect="404.htm" /> 
</customErrors>

在上面的配置中如果用户访问的页面不存在就会跳转到404.htm页面,如果用户没有权限访问请求的页面则会跳转到403.htm页面,403.htm和404.htm页面都是我们自己添加的页面,我们可以在页面中给出友好的错误提示。

 

2. 使用过滤器HandleErrorAttribute

随着ASP.NET MVC版本的更新,出现了HandleErrorAttribute,使用Filter以AOP的思想实现了针对于Action的异常处理。使用此Filter后,当程序中出现异常的时候,会去封装这些异常信息,然后路由自动转到该Controller对应的Error.cshtml中,如果此路径下没有改文件,则会到shared目录中寻找此文件。另外一个相关的是在Global.asax中的protected void Application_Error(object sender, EventArgs e)方法,是捕捉异常的最后一道防线,也就是说,这是最高层次的异常捕获处理逻辑。使用HandleErrorAttribute后,找到了Error.cshtml,则此时异常已经被捕获处理,所以不会再次被Application_Error捕获处理。此外,可以通过@model HandleErrorInfo 在Error.cshtml中定制显示异常信息。有一点需要注意的是,HandleErrorAttribute是在customErrors基础之上的,如果想使用HandleErrorAttribute,customErrors的Mode必须要设置为On或RemoteOnly.  否则,HandleErrorAttribute将不起作用。

3. 自定义显示路径

如果遇到异常后不单单是要自定义异常显示,而且还需要进行日志记录。此时,可以通过继承扩展HandleErrorAttribute来实现新的Filter,然后在lobal.ascx中对所有的Action进行注册,filters.Add(new MyHandleErrorAttribute()).在这里详细说明一下另一种方式。我们可以写一个专门的Controller和Action来进行异常发生时的路由设置.

  1. public class UtilityController : Controller  
  2.     {  
  3.         public ActionResult Error()  
  4.         {  
  5.             return View();  
  6.         }  
  7.   
  8.         public ActionResult PageNotFound()  
  9.         {  
  10.             return View();  
  11.         }  
  12. }  
public class UtilityController : Controller
    {
        public ActionResult Error()
        {
            return View();
        }

        public ActionResult PageNotFound()
        {
            return View();
        }
}

当出现异常的时候,把异常抛到最顶端,由Application_Error统一处理。这里的统一处理就包括,记录日志,重新进行页面定向等。

protected void Application_Error(object sender, EventArgs e)  
  1.         {  
  2.             var ex = Server.GetLastError();  
  3.             Log.Error(ex); //记录日志信息  
  4.             var httpStatusCode = (ex is HttpException) ? (ex as HttpException).GetHttpCode() : 500; //这里仅仅区分两种错误  
  5.             var httpContext = ((MvcApplication)sender).Context;  
  6.             httpContext.ClearError();  
  7.             httpContext.Response.Clear();  
  8.             httpContext.Response.StatusCode = httpStatusCode;  
  9.             var shouldHandleException = true;  
  10.             HandleErrorInfo errorModel;  
  11.   
  12.             var routeData = new RouteData();  
  13.             routeData.Values["controller"] = "Utility";  
  14.   
  15.             switch (httpStatusCode)  
  16.             {  
  17.                 case 404:  
  18.                     routeData.Values["action"] = "PageNotFound";  
  19.                     errorModel = new HandleErrorInfo(new Exception(string.Format("No page Found", httpContext.Request.UrlReferrer), ex), "Utility""PageNotFound");  
  20.                     break;  
  21.   
  22.                 default:  
  23.                     routeData.Values["action"] = "Error";  
  24.                     Exception exceptionToReplace = null//这里使用了EntLib的异常处理模块的一些功能  
  25.                     shouldHandleException = ExceptionPolicy.HandleException(ex, "LogAndReplace"out exceptionToReplace);  
  26.                     errorModel = new HandleErrorInfo(exceptionToReplace, "Utility""Error");  
  27.                     break;  
  28.             }  
  29.   
  30.             if (shouldHandleException)  
  31.             {  
  32.                 var controller = new UtilityController();  
  33.                 controller.ViewData.Model = errorModel; //通过代码路由到指定的路径  
  34.                 ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));  
  35.             }  
  36.         }  
  37.     }  
posted @ 2014-04-19 21:37  秋意了了  阅读(1919)  评论(0编辑  收藏  举报