New! GraphQL.NET Cookbook by Senthil Kumar Balu

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();
   }
}


Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.