• 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;
            }
        }
  • 相关阅读:
    html 之input标签height设置问题
    jquery 练习笔记
    JavaScript
    css记录
    python 反射器
    python 之 前端初识 html
    [mysql]修改
    [mysql]约束
    [mysql]select
    python_exercise_能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
  • 原文地址:https://www.cnblogs.com/imust2008/p/5306363.html
Copyright © 2020-2023  润新知