我有一个 ASP.Net MVC 3 页面,其中有一个 Html.TextAreaFor 控件,请参阅下面的代码。如果我尝试将页面提交到 http post 操作,并使用尖括号中的文本,例如:<test>
,我收到一个黄色错误屏幕,显示:
从以下位置检测到潜在危险的 Request.Form 值
客户端(RequestText =“”)。
我不明白为什么我会得到这个,因为我发现斯科特·格思里的一篇文章 http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx这说新的<%: %>
.Net 4 中的语法将自动对元素进行 HtmlEncode。由于我对 Html.TextAreaFor 控件使用 语法,因此我认为它会自动处理此问题并将尖括号转换为正确的“<”;和“>”字符串。
<% using (Html.BeginForm())
{ %>
<%: Html.ValidationSummary() %>
<h2>Enter a description of the support needed</h2>
<%: Html.TextAreaFor( m => m.RequestText, 4, 90, null) %>
<input type="submit" value="Submit" />
<% } %>
基本上现在,您正在对内容进行编码TextAreaFor
on the output。这对你没有丝毫帮助,因为你正在尝试处理input
如果您想提交“有潜在危险”的内容,您需要:
1)装饰RequestText
ViewModel 中的属性[AllowHtml]
。 (首选)
[AllowHtml]
public string RequestText { get; set; }
2)禁用validateRequest
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
然后你must确保在将数据提交到存储库层或数据库之前,在控制器中适当地清理该数据和/或对其进行编码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)