36

I am getting this error in my ASP.NET MVC application where I am taking HTML input from a WYSIWYG so I don't want the content validated.

I have attempted the solution I found here but it seems to make no difference in my MVC application. I have also tried doing it in the web.config but again - no joy.
Is this a bug in ASP.NET MVC or something?

Hakan Fıstık
  • 11,376
  • 8
  • 74
  • 105
Ryall
  • 11,272
  • 11
  • 47
  • 74
  • 1
    Kelix, the post you refer to carries with it the recommendation to use the [pages requestValidation="false"] web.config paramater or the [@Page ValidateRequest="False"] attribute of the View. Any one of these options should work fine. – David Andres Sep 21 '09 at 16:55
  • 2
    It doesn't, see: http://stackoverflow.com/questions/486408/can-a-pages-validaterequest-setting-be-overridden – Ryall Sep 21 '09 at 16:57

5 Answers5

49

In MVC you would use the ValidateInput(false) attribute.

You then need to sanitize your inputs, e.g. with something like this (built in to ASP.NET 4.5+; use NuGet package for earlier).

Craig Stuntz
  • 123,797
  • 12
  • 247
  • 268
29

In MVC 3 and later, you can also use the [AllowHtml] attribute. This attribute allows you to be more granular by skipping validation for only one property on your model.

https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.allowhtmlattribute?view=aspnet-mvc-5.2

Loren Paulsen
  • 8,080
  • 1
  • 26
  • 37
  • Worked for me, this seems safer than '[ValidateInput(false)]', thank you – MIP1983 Nov 08 '16 at 14:10
  • Had to apply to the model attributes as well the corresponding view model attributes, then it worked. – spadelives Nov 09 '16 at 19:19
  • Using this attribute [AllowHtml] in my model, got rid of the error. Will it still catch cross site scripting? – Paul Hegel Jun 12 '20 at 15:49
  • No, it will not catch cross-site scripting. Opting into this attribute either means that you will be safely encoding the contents yourself before outputting them, or you are developing an HTML editor for some sort of CMS where you actually want to allow the user to enter HTML/scripts with the intention of running them. – Loren Paulsen Jun 16 '20 at 23:55
21

Just place this attribute: [ValidateInput(false)] on the action method on the controller that handles the form post.

Pedro Jacinto
  • 946
  • 2
  • 7
  • 14
4

use <httpRuntime requestValidationMode="2.0" /> in web config

JGilmartin
  • 7,352
  • 12
  • 59
  • 79
1

In your controller action method, (the one which is bringing this) add [ValidateInput(false)]

Example

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Insert(FormCollection formCollection, Models.Page page)
    {
        //your code
        return View();
    }
yogihosting
  • 3,745
  • 2
  • 31
  • 54