MVC HttpRequestValidationException exception

ぐ巨炮叔叔 提交于 2019-12-04 05:07:18

问题


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;
            }
        }
    }


来源:https://stackoverflow.com/questions/28851309/mvc-httprequestvalidationexception-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!