在web config 里做如下的修改
(1) 在page属性里添加如下的内容 validateRequest="false" enableSessionState="true" enableViewState="false">
(2)在</page>后面添加如下代码 <httpRuntime requestValidationMode="2.0"/> <!-- 这也是新添加的-->
<pages validateRequest="false" enableSessionState="true" enableViewState="false">
<!--pages的三个属性是为了消除有潜在危险的Request.Form值 -->
<namespaces >
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
<httpRuntime requestValidationMode="2.0"/> <!-- 这也是新添加的-->
</system.web>
在做过上述的修改之后,再次重新生成程序。
(3) 如果经过上述两步操作之后 还有错误 那就进行如下操作 在你的方法上添加 属性 [ValidateInput(false)]
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(Message message)
{
if (ModelState.IsValid)
{
db.Entry(message).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(message);
}
英文注解:when submitting an http post containing the < character mvc blocks this and reports a potentially dangerous script.
This can be overcome in .net 3.5 by adding the controller directive [ValidateInput(false)]
This does not work in .net 4.0 so it is not possible to post the contents of a textarea containing the < character.