65.9K
CodeProject is changing. Read more.
Home

Try catch block around "A potentially dangerous Request.Form"

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Nov 3, 2011

CPOL
viewsIcon

37973

Try catch block around "A potentially dangerous Request.Form"

Sometimes, the user enters HTML tags or malicious characters in text fields. By default, ASP.NET blocks those characters and shows the following error:
A potentially dangerous Request.Form value was detected from the client (aa="<te>").
If we want to show a user friendly message on the raise of this exception, then we have to catch that particular type of exception. But the question is, from where we can catch that exception where we place our try catch block to catch this type of exception. I have seen lot of solutions which are mostly using global.asax where they handle this exception on Application_Error. But what if we want to catch this type of error on Page. Here is the solution:
protected override System.Collections.Specialized.NameValueCollection DeterminePostBackMode()
    {
       try
        {
            return base.DeterminePostBackMode();
        }
        catch(HttpRequestValidationException ex) {
           
//            do the handling here
            
        }
return null;
    }