-->

MVC HttpRequestValidationException exception

2019-02-27 21:06发布

问题:

I am coding a MVC 5 internet application and have a question in regards to the HttpRequestValidationException exception.

My previous code in my controller is as follows:

protected override void OnException(ExceptionContext filterContext)
{
    // Make use of the exception later
    this.Session["ErrorException"] = filterContext.Exception;

    if (filterContext.Exception is HttpRequestValidationException)
    {
        TempData["UITitle"] = "Validation";
        TempData["UIHeading"] = customErrorType;
        TempData["UIMessage"] = filterContext.Exception.Message;
        TempData["UIException"] = filterContext.Exception;
        filterContext.ExceptionHandled = true;
    }
    else
    {
        TempData["UITitle"] = "Error";
        TempData["UIHeading"] = customErrorType;
        TempData["UIMessage"] = filterContext.Exception.Message;
        TempData["UIException"] = filterContext.Exception;
    }

    filterContext.Result = this.RedirectToAction("Index", "Error");

    base.OnException(filterContext);

}

If an exception occurred, then the Index view in the Error controller displayed this error. I have now written the following global filter:

public class ExceptionFilterDisplayErrorView : IExceptionFilter
{
    public virtual void OnException(ExceptionContext filterContext)
    {
        filterContext.ExceptionHandled = true;

        RouteValueDictionary routeValueDictionary = new RouteValueDictionary();
        routeValueDictionary.Add("controller", "Error");
        routeValueDictionary.Add("action", "Index");

        filterContext.Controller.TempData.Clear();

        filterContext.Controller.TempData.Add("UITitle", "Error");
        filterContext.Controller.TempData.Add("UIHeading", "Error");
        filterContext.Controller.TempData.Add("UIMessage", filterContext.Exception.Message);
        filterContext.Controller.TempData.Add("UIException", filterContext.Exception);

        RedirectToRouteResult redirectToRouteResult = new RedirectToRouteResult(routeValueDictionary);
        filterContext.Result = redirectToRouteResult;                    
    }
}

The above filter works the same as the previous OnException function, except now, if a HttpRequestValidationException exception occurs, the default stack trace page is shown, rather than the Error controller view.

Is it possible to display a custom error view for HttpRequestValidationException exceptions in an exception filter?

回答1:

Something like this works for me.

public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
        {
            public void OnException(ExceptionContext filterContext)
            {
                if (!filterContext.ExceptionHandled)
                {
                    int val = (int)(((Exception)filterContext.Exception).ActualValue);


                    filterContext.Result = new ViewResult
                    {
                        ViewName = "CustomError",
                        ViewData = new ViewDataDictionary<int>(val)
                    };

                    filterContext.ExceptionHandled = true;
                }
            }
        }

** EDIT ***

public class HttpRequestValidationExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled && filterContext.Exception is HttpRequestValidationException)
            {
                IDictionary val = filterContext.Exception.Data;


                filterContext.Result = new ViewResult
                {
                    ViewName = "RangeError",
                    ViewData = new ViewDataDictionary<IDictionary>(val)
                };

                filterContext.ExceptionHandled = true;
            }
        }
    }