在 asp mvc 中从客户端检测到一个潜在危险的 Request.Form 值

A potentially dangerous Request.Form value was detected from the client in asp mvc

我正在使用 Rich Editor

它告诉我这个错误

Return 空 t.FullText = HttpContext.Request["Editor1"];

查看:

<div class="name">
    <label id="lbl-full-text">FullText : </label>
  @* @Html.TextAreaFor(model => model.FullText)*@
    <div name="FullText" style="margin-right: 109px;    margin-top: -34px;">
        @Html.Raw(ViewBag.Editor)
    </div>
</div>

控制器:

[HttpPost, ValidateInput(false)]
public ActionResult CreateNews(Tbl_News t, HttpPostedFileBase pic, int Category = 0)
{
    string content = Request.Unvalidated["Editor1"];
    .
    .
    .
    t.FullText = HttpContext.Request["Editor1"];

}

元数据:

[AllowHtml]
public string FullText { get; set; }

我该如何解决这个问题?

有几种方法可以解决这个问题。在我看来,并非所有方法都是最好的方法。

这里可能是最差的

霰弹枪接近

<httpRuntime requestValidationMode="2.0"/>

稍微好一点,因为你可以在特定页面上关闭它

<configuration>
        <system.web>
            <pages validateRequest="false" />
        </system.web>
</configuration>

在这里您可以为网站的特定部分关闭它

<configuration>
...
<location path="MyFolder/.aspx">
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
</location>
...
</configuration>

如果您使用的是 mvc,另一种方法是将验证输入属性添加到您的操作中:

[ValidateInput(false)]

我已经有一段时间没有在我创建的任何网站上执行这些操作了,但我认为它们仍然有效。

使用 System.Web.HttpContext.Current.Request.Unvalidated.Form["Editor1"] 禁用整个页面或所有页面的输入验证是个坏主意!

我最终通过在 AJAX 发布之前截取 jQuery 中编辑的值并转义值来完成这项工作,例如:

$("#Textareaeditor").val(escape(-get value from editor-);

然后,服务器端你只需要做:

model.property = URI.unEscapeDataString(model.property).

这对 md 有效,但我不想设置 requestValidationMode=2.0