• HandleErrorAttribute 特性使用


     public class EwHandleErrorAttribute : HandleErrorAttribute
        {
            public override void OnException(ExceptionContext filterContext)
            {
                if (ICTConfiguration.Debug)
                {
                    base.OnException(filterContext);
                    return;
                }
    
                if (filterContext.ExceptionHandled)
                {
                    return;
                }
                if (filterContext.HttpContext.Response.IsRequestBeingRedirected)
                {
                    return;
                }
                var httpCode = new HttpException(null, filterContext.Exception).GetHttpCode();
                if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
                {
                    return;
                }
                if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
                {
                    return;
                }
                ExceptionHelper.LogException(filterContext.Exception, HttpContext.Current);
                bool isAjaxCall = string.Equals("XMLHttpRequest", filterContext.HttpContext.Request.Headers["x-requested-with"],
                                           StringComparison.OrdinalIgnoreCase);
                if (isAjaxCall)
                {
                    string message = filterContext.Exception.Message;
                    if (filterContext.Exception is HttpRequestValidationException)
                    {
                        message = "包含非法字符";
                    }
    
                    filterContext.Result = new JsonResult()
                    {
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                        Data = new
                        {
                            succeed = false,
                            ret = httpCode,
                            msg = message
                        }
                    };
                }
                else
                {
                    var controllerName = (string)filterContext.RouteData.Values["controller"];
                    var actionName = (string)filterContext.RouteData.Values["action"];
                    var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                    filterContext.Result = new ViewResult()
                     {
                         ViewName = View,
                         MasterName = Master,
                         ViewData = new ViewDataDictionary(model),
                         TempData = filterContext.Controller.TempData
                     };
                    filterContext.HttpContext.Response.Redirect("/500.html");
                }
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                filterContext.HttpContext.Server.ClearError();
            }
        }

    global注册过滤器

     public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
                filters.Add(new JoinEnterpriseAttribute(), 1);
                filters.Add(new EwHandleErrorAttribute(), 2);
            }
  • 相关阅读:
    软件工程结课作业
    十三次作业
    十二次作业
    十一次作业
    十次作业
    找回感觉的练习
    CSS背景样式和列表样式
    盒子模型案例应用
    display属性
    margin外边距属性
  • 原文地址:https://www.cnblogs.com/kingCpp/p/4646150.html
Copyright © 2020-2023  润新知