Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't turn off Request Validation for an ASP.NET MVC Controller

I am trying to turn off Request Validation for all action methods in a controller by doing this:

[ValidateInput(false)]
public class MyController : Controller
{
    ...

The reference I am using says this is possible and tells me to do it this way, but for some reason it's not working.

If I submit any html (even a simple <b> tag) through a text box, I get the error:

A potentially dangerous Request.Form value was detected from the client (text=<b>").

It's also not working by attaching the attribute to an individual method.

How can I disable Request Validation for a controller?

EDIT

I am working in VS2008 built in test server.

like image 508
Ronnie Overby Avatar asked Jul 10 '09 15:07

Ronnie Overby


People also ask

How can disable validation on button click in ASP.NET MVC?

To disable validation when clicking a button, set the MVC Button. CausesValidation property value to false.

Which attribute can be used to disable request validation for a property?

You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section.

What is enable and disable client side validation in ASP.NET MVC?

We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level. For client-side validation, the values of above both the keys must be true.

How can you disable session of a particular controller in MVC?

We can disable the session for those controllers using the SessionStateAttribute (set session state Behavior to Disabled) and we can get a slight performance improvement for our application.


3 Answers

Pro ASP.NET MVC Framework (p466) says the following is supposed to work:

public class MyController : Controller 
{
     public MyController() {
        ValidateRequest = false;
     }
}
like image 113
keithm Avatar answered Oct 09 '22 21:10

keithm


I tested it on my machine, on both the class definition and the action method, and it worked for me in both cases. Are you sure your view lines up with your method/controller? Are you putting the attribute on the GET method or the POST method?

[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]   
public ActionResult MyAction (int id, string content) {   
    // ...   
}
like image 23
Robert Harvey Avatar answered Oct 09 '22 21:10

Robert Harvey


To make it working you need to modify web.config as well:

<system.web>
    <httpRuntime requestValidationMode="2.0"/>
    ...
</system.web>
like image 32
the_joric Avatar answered Oct 09 '22 19:10

the_joric