MVC 异常过滤器

本文实现对MVC异常处理功能,借此来讨论ASP.NET MVC中拦截器的使用方法。

第一步:写一个自己的异常过滤器,让它继承HandleErrorAttribute类 然后重写OnException()这个方法  (注:HandleErrorAttribute 类已经继承了FilterAttribute, IExceptionFilter这两个接口,并实现了它们,所以这我们只要继承HandleErrorAttribute就可以了)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Itcast.CMS.WebApp.Controllers.Filters
{
    public class MyExceptionFilterAttribute : HandleErrorAttribute
    {

        Itcast.CMS.Common.LogHelper log = new Common.LogHelper();
        public override void OnException(ExceptionContext filterContext)
        {
            
            //获取或设置一个值,该值指示是否已处理异常。如果已处理异常,则为 true;否则为 false。
            if (filterContext.ExceptionHandled==true)
            {
                HttpException httpException = filterContext.Exception as HttpException;
                //为什么要特别强调500 因为MVC处理HttpException的时候,如果为500 则会自动
                //将其ExceptionHandled设置为true,那么我们就无法捕获异常
                if (httpException.GetHttpCode() == 500)
                {
                    return;
                }                
            }
            //创建一个HttpException对象,使用filterContext.Exception来对它初始化(其实就是将filterContext.Exception转换成HttpException类型)
            HttpException httpExce = new HttpException(null, filterContext.Exception);
            //HttpException httpExce = filterContext.Exception as HttpException;
            
            if (httpExce != null)
            {
                if (httpExce.GetHttpCode() == 404)
                {
                    HttpContext.Current.Response.Redirect("/Error/NotFound");//跳转到404错误页面
                }
                else if (httpExce.GetHttpCode() == 500)
                {
                    //也可以这样写
                    filterContext.HttpContext.Response.Redirect("/Error/ServerError");//跳转到500服务器错误页面
                }
            }
           
            //调用log4net来记录日记
            log.Error("错误:" + filterContext.Exception.Message, filterContext.Exception);

            filterContext.ExceptionHandled = true;    //将异常设置为已处理     
            
        }

    }
}

第二步:将自己刚刚写的过异常滤器注册到MVC的全局过滤器中去。只要到App_Start这个文件夹下的FilterConfig.cs文件中注册一下就可以,

将默认的 filters.Add(new HandleErrorAttribute()); 这段代码 替换为filters.Add(new MyExceptionFilterAttribute()); 具体代码如下

using Itcast.CMS.WebApp.Controllers.Filters;
using System.Web;
using System.Web.Mvc;

namespace Itcast.CMS.WebApp
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new MyExceptionFilterAttribute());
        }
    }
}

 做好以上两步,当MVC中有异常的时候,就会自动跳转到自己的过滤器中进行处理了。(注:不需要在类,或者方法前面加[MyExceptionController])只要做好前面两步就可以了


其实有的时候报404错误,也就是在路由阶段就没有通过,也就是没有到达控制器,所以无法进入到我们的过滤器。(去网上查了一下,都说404不是属于一个异常,仅仅是路径错误,这时候只要去Web.cofig配置文件中,配置一下,当出现404错误的时候,就跳转到我们自定义的错误页去)

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.web>
   
    <!--定义友好的错误,出错默认导向到 我们定义好的友好错误页面 -->

    <!--mode属性:mode="On"的时候就表示对所以访问者都显示定制错误页;mode="Off"就表示关闭定制的错误页,页面错误的时候将本地和远程用户都会看到详细的错误信息;mode="RemoteOnly"就表示本地用户将看到详细错误信息(这里的本地的意思即:你直接登陆网站服务器上),而远程用户将会看到程序员定制的错误页。所以一般情况下我们都建议将mode属性设为RemoteOnly-->

    <!--RedirectMode 属性:获取或设置一个值,该值指示在将用户重定向到自定义错误页面时,是否应更改请求的 URL。默认值为 ResponseRedirect。如果 RedirectMode 属性设置为 ResponseRedirect,则将用户重定向到该错误页面,并且原始 URL 更改为该错误页面的 URL。如果 RedirectMode 属性设置为 ResponseRewrite,则将用户定向到错误页面,并且不更改浏览器中的原始 URL。【ResponseRedirect其实就是Response.Redirect;而ResponseRewrite其实就是Server.Transfer】 -->

    <!--defaultRedirect属性:应用程序在发生错误时重定向到的默认URL-->
    <!--<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/Error/notfound">-->
      
    <!--在它下面还可以分的很详细,可以针对一些错误码来指向我们针对这些错误码而定制的页面-->
    <!--<error statusCode="403" redirect="/Error/403" />-->
    <!--<error statusCode="404" redirect="/Error/404" />-->
    <!--</customErrors>-->

    <customErrors mode="On" >
       <error statusCode="404" redirect="/Error/notfound"/><!--出现404错误的时候跳转到Error控制器下的notfound方法-->
    </customErrors>
  
  </system.web>
 
</configuration>




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。