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