HandleErrorAttribute not working

前端 未结 3 1708
余生分开走
余生分开走 2020-12-18 05:58

I have started an MVC 3 template project in VS10 and modified global.asax.cs as such:

public static void RegisterGlobalFilters(GlobalFilterCollection filters         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 06:36

    Do check fear's answer below. It's certainly the simpler, if it works.

    Since it came after a few weeks, this is how my filter finally spelled out, using Darins response and incorporating Elmah-reporting, with code from this topic.

    I still don't know why you can't set properties on a global action filter.

    public class MyHandleErrorAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.IsChildAction &&
                (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled))
            {
                var innerException = filterContext.Exception;
                if ((new HttpException(null, innerException).GetHttpCode() == 500))
                {
                    var viewName = "GeneralError";
                    if (typeof (HttpAntiForgeryException).IsInstanceOfType(innerException))
                        viewName = "SecurityError";
    
                    var controllerName = (string) filterContext.RouteData.Values["controller"];
                    var actionName = (string) filterContext.RouteData.Values["action"];
                    var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                    var result = new ViewResult
                                            {
                                                ViewName = viewName,
                                                ViewData = new ViewDataDictionary(model),
                                                TempData = filterContext.Controller.TempData
                                            };
    
                    filterContext.Result = result;
                    filterContext.ExceptionHandled = true;
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 500;
                    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    
     //From here on down, this is all code for Elmah-reporting.
                    var version = Assembly.GetExecutingAssembly().GetName().Version;
                    filterContext.Controller.ViewData["Version"] = version.ToString();
    
                    var e = filterContext.Exception;
                    if (!filterContext.ExceptionHandled // if unhandled, will be logged anyhow
                        || RaiseErrorSignal(e) // prefer signaling, if possible
                        || IsFiltered(filterContext)) // filtered?
                        return;
    
                    LogException(e);
                }
            }
        }
    
        private static bool RaiseErrorSignal(Exception e)
        {
            HttpContext context = HttpContext.Current;
            if (context == null)
                return false;
            var signal = ErrorSignal.FromContext(context);
            if (signal == null)
                return false;
            signal.Raise(e, context);
            return true;
        }
    
        private static bool IsFiltered(ExceptionContext context)
        {
            var config = context.HttpContext.GetSection("elmah/errorFilter")
                         as ErrorFilterConfiguration;
    
            if (config == null)
                return false;
    
            var testContext = new ErrorFilterModule.AssertionHelperContext(
                context.Exception, HttpContext.Current);
    
            return config.Assertion.Test(testContext);
        }
    
        private static void LogException(Exception e)
        {
            HttpContext context = HttpContext.Current;
            ErrorLog.GetDefault(context).Log(new Error(e, context));
        }
    }
    

提交回复
热议问题