Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Georg Patscheider for How to catch HttpRequestValidationException in production

Another way that only works with MVC is using a custom Exception Filter:

  • Create a custom FilterAttribute that implements IExceptionFilter
  • from inside the FilterAttribute, you can redirect to the controller or view to be used to display the error.
  • register the filter in the Global.asax or attribute your controllers

This has the advantage that you can use the normal MVC infrastructure (Razor) to render the error view.

public class HttpRequestValidationExceptionAttribute : FilterAttribute, IExceptionFilter {    public void OnException(ExceptionContext filterContext) {        if (!filterContext.ExceptionHandled && filterContext.Exception is HttpRequestValidationException) {            filterContext.Result = new RedirectResult("~/HttpError/HttpRequestValidationError");            filterContext.ExceptionHandled = true;        }    }}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>