I am trying to prevent having this raw error appear on the screen when the user enters e.g. pointed brackets in an input field:
[System.Web.HttpRequestValidationException] A potentially dangerous Request.Form value was detected from the client (Message="
I've learned in posts like this one that I should be able to catch and handle this error in the void Application_Error(object sender, EventArgs e)
method in the Global.asax.cs
file, e.g. I have this code:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpRequestValidationException)
{
Response.Clear();
Response.StatusCode = 200;
Response.Write(@"<html><body>HTML is not allowed.</body></html>");
Response.End();
}
}
But regardless of what code I have in the void Application_Error(object sender, EventArgs e)
method, code execution does not enter it, but rather simply outputs the raw error as before.
What could be preventing code execution from entering the void Application_Error(object sender, EventArgs e)
method?
Note: I don't have custom errors set to RemoteOnly in my Web.config
as seems to be the issue in Is global.asax Application_Error event not fired if custom errors are turned on?