HomeASP.NETASP.NET MVC – A potentially dangerous Request.Form value was detected from the client

ASP.NET MVC – A potentially dangerous Request.Form value was detected from the client

When trying to accept an HTML tag as input for one of the textbox in my ASP.NET MVC and saving the data , I received the below error .

A potentially dangerous Request.Form value was detected from the client

How to Solve the error A potentially dangerous Request.Form value was detected from the client in ASP.NET MVC ?

One option is to set the validateRequest=”false” and/or add the following in the Web.config file under System.web tag.

<configuration>
    <system.web>
        <pages validateRequest="false" />
    </system.web>
</configuration>

<httpRuntime requestValidationMode="2.0"/>

If you want to display an proper error message when this error occurs , you can catch the exception in the Application error in the Global.asax file , identify if the error was HttpRequestValidationException and provide an custom error message in response.

void Application_Error(object sender, EventArgs e)
{
   Exception ex1 = Server.GetLastError();
   if (ex1 is HttpRequestValidationException)
   {
       Response.Clear();
       Response.StatusCode = 200;
       Response.Write(@"[html]");
       Response.End();
   }
}


Leave a Reply

You May Also Like

You can read connection string from web.config file in ASP.NET by using the ConfigurationManager class in case you want to...
When you deploy your ASP.NET Web Application on Windows Server running IIS , there are times that you might receive...
This post will explain how to resolve the error “Handler “aspNetCore” has a bad module “AspNetCoreModuleV2” in its module list”...