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

Your email address will not be published. Required fields are marked *

You May Also Like

You can pass an array of integer to an ASP.NET Web Web API REST service by setting the [FromUri] attribute...
One of the common ways to refresh webpage every few mins is by using JavaScript or HTML Meta tag. For...
This post will provide the list of some of the top ASP.NET based Content Management Systems (CMS) that you may...