0

I am using below code to catch HttpRequestValidationException. But the problem is it navigates to different Page.

I want to show "Invalid Input" Error Message on the same page where user is entering the information and clicking on Submit button.

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>Invalid Input</body></html>");
        Response.End();
        return;
    }
}
Bokambo
  • 4,204
  • 27
  • 79
  • 130

1 Answers1

-1

You can use a try/catch block:

protected void Application_Error()
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();

    try
    {
        //Operations you need to perform
    }
    catch (HttpRequestValidationException)
    {
      //Output Error
    }
}
thetipsyhacker
  • 1,402
  • 4
  • 18
  • 39