5

How can i redirect the user to a custom error page, when HttpRequestValidationException occurred. I tried to catch it in my basecontroller :

protected override void OnException(ExceptionContext exceptionContext)
    {
      if (exceptionContext.Exception is HttpRequestValidationException)
        {
          this.View("CustomError").ExecuteResult(this.ControllerContext);
        }
    }

But i still get the exception : A potentially dangerous request.form value was detected from the client

Djave
  • 277
  • 1
  • 5
  • 14

1 Answers1

4

This exception occurs much earlier in the execution of the request and cannot be handled by the OnExceptionmethod in a base controller. You could write a global exception handler as I showed in this post.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • If i add the code Server.ClearError(); in protected override void OnException(ExceptionContext exceptionContext) action in basecontroller, then it works. But what is best practice? to catch this exception in global asax or in my basecontroller? – Djave Apr 09 '13 at 09:07
  • A global exception handler allows you to handle much wider range of exceptions that are occurring even outside your controllers. So that's what I would opt for. – Darin Dimitrov Apr 09 '13 at 13:29