zoukankan      html  css  js  c++  java
  • 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);
            }
  • 相关阅读:
    联网获取图片, 保存用户的图像 bitmap
    使用 线程池,控制线程 , 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待:
    ImageView属性 android:scaleType="fitXY" ,拉伸图片非常好用
    Activity之间传值,Intent
    退出应用程序 按两次退出键
    修炼-------------Android TabHost,TabWidget选项卡总结
    对TabHost、TabWidget的理解分析
    poj 1847 Tram
    hdu 1874 畅通工程续
    hdu 2544 最短路
  • 原文地址:https://www.cnblogs.com/kingCpp/p/4646150.html
Copyright © 2011-2022 走看看