Results 1 to 6 of 6

Thread: How do we handle HttpRequestValidationException Exception

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    How do we handle HttpRequestValidationException Exception

    Hi,

    I have some textboxes on my aspx page. If the user puts "<" or ">" character by mistake in those boxes, it shows the System.Web.HttpRequestValidationException error.
    Code:
    System.Web.HttpRequestValidationException: 
    A potentially dangerous Request.Form value was detected from the client 
    ...
    I don't want to turn off ValidateRequests.
    So I tried catching it in the Page_Error event, but that event is never fires on this exception.
    I also tried overriding the OnError, but without success.

    How can I catch this exception and show the user a friendly message?

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do we handle HttpRequestValidationException Exception

    You'll need to catch it in your Application_Error event in global.asax because this occurs before page processing.

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How do we handle HttpRequestValidationException Exception

    Quote Originally Posted by mendhak
    You'll need to catch it in your Application_Error event in global.asax because this occurs before page processing.
    But this is just for one of the pages. So the application level events don't seem like the appropriate place for that. Also will I be able to continue showing the page after that error?

    I have a Panel with a Label in it on the top of my page where I display any errors I encounter. Is it somehow possible possible to show a message in that label?
    Something like: "< & > are not allowed in these boxes." etc.

    I found many resources on the internet that say that it should be handled in the Page_Error event or the Page OnError method should be overridden. I wonder how it worked for them but not for me?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do we handle HttpRequestValidationException Exception

    Partially true. You can catch it on your page but the page will not continue execution after that.

    That is why the recommended practice is to either handle it in global.asax and display a simple message or disable event validation and perform the stripping out/checking for HTML yourself.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do we handle HttpRequestValidationException Exception

    Partially true. You can catch it on your page but the page will not continue execution after that.

    That is why the recommended practice is to either handle it in global.asax and display a simple message or disable event validation and perform the stripping out/checking for HTML yourself.

  6. #6
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: How do we handle HttpRequestValidationException Exception

    You could use javascript to test for <> and catch the error on the client... Also it's not so bad to handle that error globally and provide a specific error message. Generally if a user enters html tags/script that's not allowed it's not an innocent mistake... generally speaking...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width