【问题标题】:Return View After Post with ModelState.IsValid == false with AllowHtml on property使用 ModelState.IsValid == false 和 AllowHtml 属性发布后返回视图
【发布时间】:2020-04-11 11:30:12
【问题描述】:

我的 ViewModel / Action / Controller 采用以下形式:

[HttpPost, ValidateInput(false)]
public ActionResult TheActionMethod(TheViewModel aViewModelVariable)
{
     if (ModelState.IsValid)
     {
        //Do the things
        return AnActionThatListsAllItemsOfSimilarTypeAndGivesSuccessMessage();  //Can get here
     }
     else
     {
        //Do other things 
     }
     return View(aViewModelVariable); //Error occurs if this line executes
}

而我的视图模型的形式为:

[Display(Name = "Does not allow HTML")]
public string AFieldThatDoesntAllowHtml{ get; set; }
[Required]
[Display(Name = "Allows HTML")]
[AllowHtml]
public string AFieldThatAllowsHtml{ get; set; }

我可以很好地提交,假设一切都验证并保存等。 如果 !ModelState.isValid 出现问题,并且代码到达 return View(aViewModelVariable);我总会得到:

System.Web.HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (AFieldThatAllowsHtml="<div>SomeText That was entered...")

我希望不用解决这个问题

<httpRuntime requestValidationMode="2.0" />

我也希望能够在没有 ValidateInput(false) 的情况下执行此操作

【问题讨论】:

    标签: c# razor asp.net-mvc-5


    【解决方案1】:

    如果以 JSON 格式传递参数,则不会触发 HttpRequestValidationException。

    但是,您可能应该执行自定义验证以从任何潜在危险的 html 标记中去除字符串内容。

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 2022-09-25
      • 2011-02-22
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2018-02-16
      • 2012-11-02
      相关资源
      最近更新 更多