• MVC四大筛选器—ExceptionFilter


    该筛选器是在系统出现异常时触发,可以对抛出的异常进行处理。所有的ExceptionFilter筛选器都是实现自IExceptionFilter接口  

        public interface IExceptionFilter
        {
            void OnException(ExceptionContext filterContext);
        }

    实现OnException方法来实现对异常的自定义处理

    MVC4中实现了默认的异常处理机制,源码如下 

    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;
            }
    Application_Start中将HandleErrorAttribute添加到全局筛选器GlobalFilterCollection中,系统即会对异常进行对应的处理。
    我们现在实现一个自定义的异常处理筛选器,在处理完后记录异常信息至日志文件中  
     public class MyExceptionHandleAttribute : HandleErrorAttribute
        {
            public MyExceptionHandleAttribute()
                : base()
            {
            }
    
            public void OnException(ExceptionContext filterContext)
            {
                base.OnException(filterContext);
                //记录日志
                log.Info(filterContext.Exception);
            }
        }
    在GlobalFilterCollection添加MyExceptionHandleAttribute 即可使用自定义的异常筛选器来处理
  • 相关阅读:
    获取到某一方法的调用者的类名、方法名、命名空间(转)
    JQ插件收集
    输出KnownColor枚举颜色值
    SQL 优化之该走索引却不走索引的分析
    oracle日期处理(一)
    ORACLE如何使用DBMS_METADATA.GET_DDL获取DDL语句
    案例分析:ora04031与ora04030错误分析与解决
    分区表、分区索引和全局索引部分总结
    Oracle性能调整指导纲要
    ORACLE常用SQL优化hint语句
  • 原文地址:https://www.cnblogs.com/oneheart/p/4131126.html
Copyright © 2020-2023  润新知