zoukankan      html  css  js  c++  java
  • HandleErrorAttribute只能处理httpStatusCode为500的异常(服务器异常)

    HandleErrorAttribute源代码:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
        public class HandleErrorAttribute : FilterAttribute, IExceptionFilter
        {
            private const string DefaultView = "Error";
            private readonly object _typeId = new object();
            private Type _exceptionType = typeof(Exception);
            private string _master;
            private string _view;
            public Type ExceptionType
            {
                get
                {
                    return this._exceptionType;
                }
                set
                {
                    if (value == null)
                    {
                        throw new ArgumentNullException("value");
                    }
                    if (!typeof(Exception).IsAssignableFrom(value))
                    {
                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, MvcResources.ExceptionViewAttribute_NonExceptionType, new object[]
                        {
                            value.FullName
                        }));
                    }
                    this._exceptionType = value;
                }
            }
            public string Master
            {
                get
                {
                    return this._master ?? string.Empty;
                }
                set
                {
                    this._master = value;
                }
            }
            public override object TypeId
            {
                get
                {
                    return this._typeId;
                }
            }
            public string View
            {
                get
                {
                    if (string.IsNullOrEmpty(this._view))
                    {
                        return "Error";
                    }
                    return this._view;
                }
                set
                {
                    this._view = value;
                }
            }
            public virtual void OnException(ExceptionContext filterContext)
            {
                if (filterContext == null)
                {
                    throw new ArgumentNullException("filterContext");
                }
                if (filterContext.IsChildAction)
                {
                    return;
                }
                if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
                {
                    return;
                }
                Exception exception = filterContext.Exception;
                if (new HttpException(null, exception).GetHttpCode() != 500)//注意这里
                {
                    return;
                }
                if (!this.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 = this.View,
                    MasterName = this.Master,
                    ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                    TempData = filterContext.Controller.TempData
                };
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.StatusCode = 500;
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
        }
  • 相关阅读:
    03-架构设计的目的
    02-架构设计的历史背景
    01-架构到底是指什么
    python 基础 1.5 python数据类型(四)--字典
    python 基础 1.5 python数据类型(三)--元组常用方法示例
    python 基础 1.5 python数据类型(三)--元组
    python 基础 1.5 python数据类型(二)--列表常用方法示例
    python 基础 1.5 数据类型(二)--列表
    python 基础 1.5 python 数据类型(一)--整型 浮点型 布尔型及字符串和常用方法
    python 基础 1.4 python运算符
  • 原文地址:https://www.cnblogs.com/imust2008/p/5306363.html
Copyright © 2011-2022 走看看