How to catch HttpRequestValidationException in production

Posted by bruno on Stack Overflow See other posts from Stack Overflow or by bruno
Published on 2011-02-17T11:16:06Z Indexed on 2011/02/17 15:25 UTC
Read the original article Hit count: 327

Filed under:
|
|
|
|

Hello all,

I have this piece of code to handle the HttpRequestValidationException in my global.asax.cs file.

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        Response.Clear();
        Response.StatusCode = 200;
        Response.Write(@"<html><head></head><body>hello</body></html>");
        Response.End();
        return;
    }
}

If I debug my webapplication, it works perfect. But when i put it on our production-server, the server ignores it and generate the "a potentially dangerous request.form value was detected from the client" - error page. I don't know what happens exactly... If anybody knows what the problem is, or what i do wrong..?

Also I don't want to set the validaterequest on false in the web.config.

The server uses IIS7.5, And I'm using asp.net 3.5.

Thanks, Bruno

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET