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