Asp.net表單驗證功能是為了防止http請求中包含惡意內容,如html,js。
當業務需要允許錄入此類內容時可以做一下設置:
1.關閉表單的驗證([ValidateInput(false)])
[HttpPost] [ValidateInput(false)] public ActionResult Edit(string comment) { if (ModelState.IsValid) { // Etc. } return View(comment); }
2.單獨設置某個表單屬性不做驗證([AllowHtml])
class Info{ public int Id {get;set;} [AllowHtml] public string Prop1 { get; set; } } [HttpPost]
public ActionResult Edit(Info info) { if (ModelState.IsValid) { // Etc. } return View(comment); }
3.當使用Rquest.Form時(Unvalidated)
[HttpPost] public ActionResult Edit() { var rawComment = Request.Unvalidated.Form["comment"]; return View(); }
另外,注意web.config有一處配置為前提
<system.web> <httpRuntime requestValidationMode="2.0" /> </system.web>