首页 > 其他 > 详细

HandleErrorAttribute 特性使用

时间:2015-07-14 20:11:54      阅读:174      评论:0      收藏:0      [点我收藏+]
 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);
        }

 

HandleErrorAttribute 特性使用

原文:http://www.cnblogs.com/kingCpp/p/4646150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!