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(@"");
       Response.End();
   }
}


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

Passing Array of Integers to ASP.NET Web API REST Service
You can pass an array of integer to an ASP.NET Web Web API REST service by setting the [FromUri] att...
Webinar - Best Practices - Optimizing ASP.NET Performance, Part 1
DevExpress is hosting a webinar on the topic "Best Practices - Optimizing ASP.NET Performance, Part ...
Contact Form 7 Wordpress Plugin - Error Failed to send your message. Please try later or contact administrator by other way
Contact Form 7 plugin is one of the popular word press plugin that allows us to customize and perfor...