Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Request.Unvalidated with ASP.NET MVC 4?

According to this link, now we can use Request.Unvalidated to access a raw value of a form field wihout triggering request validation (and see dreadful error message A potentially dangerous Request.Form...). Unfortunately I could not get it work.

Web.config

<httpRuntime targetFramework="4.5" requestValidationMode="4.5" />

A simple field in view model:

// [AllowHtml] - even I tried this, it still did not work :(
public string Description { get; set; }

And controller action:

[HttpPost]
public ActionResult Edit([Bind(Prefix = "Edit")] EditModel model)
{
  string s = Request.Unvalidated.Form["Edit.Description"];
}

I still see error "A potentially dangerous Request.Form...", why? Tried google but there was no example with ASP.NET MVC.

Toolbox: I'm working with a ASP.NET MVC 4 project, targetting .NET 4.5, and VS2012.

Thanks,

UPDATE: Using AllowHtml with Description property fixed my problem, even don't need Request.Unvalidated. There is still an issue as I commented below in @webdeveloper's answer.

like image 838
Tien Do Avatar asked Nov 23 '25 02:11

Tien Do


2 Answers

Look at this links:

  • Request Validation in ASP.NET
  • Understanding Request Validation in ASP.NET MVC 3
  • ASP.NET MVC Tip #48 – Disable Request Validation

They will help you to understand, how does it works.

like image 51
webdeveloper Avatar answered Nov 28 '25 16:11

webdeveloper


It looks like you need to set the request validation mode to 2.0 as described in webdeveloper's first link. Try this:

<httpRuntime targetFramework="4.5" requestValidationMode="2.0" />
like image 37
Evan M Avatar answered Nov 28 '25 15:11

Evan M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!