This is an ASP.Net MVC 5
project
I have a simple model whose one of its properties allows a HTML
input:
public class FooModel {
//other properties
[AllowHtml]
public string BarField { get; set; }
}
And a controller which uses the model as shown below:
[OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)]
public class FooController : Controller {
//some other codes...
// GET: Foo/Create
public ActionResult Create(int? id, int number = 0) {
//some code
}
// POST: Foo/Create
[HttpPost]
public ActionResult Create(FooModel fooModel) {
//some code
}
// GET: Foo/Edit/5
public ActionResult Edit(int? id, int number = 0) {
//some code
}
// POST: Foo/Edit/5
[HttpPost]
public ActionResult Edit(FooModel model, FormCollection collection) {
//some code
}
}
Upon reading some of the posts in SO:
I know that the following must be done to ensure the AllowHtml
attribute to work:
- use
<httpRuntime requestValidationMode="2.0" />
in the web.config - clear up cache of the controller where the model is passed and used
[OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)]
Thus, I have the following complete element for my <system.web>
in the web.config:
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.5" requestValidationMode="2.0" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
And, as you can see, I also have put the OutputCache
attribute on top of the controller:
[OutputCache(NoStore = true, Duration = 0, Location = OutputCacheLocation.None)]
Now, this works very well for Create
Action (that is, I can insert HTML element in the BarField
and the post is accepted and the Action is called without problem).
But when I do the Edit
Action, the action is not even called and the error:
A potentially dangerous Request.Form value was detected from the client ( BarField="...words here <i>and also here</i><...").
Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874.
is shown on the page. Why is this so?