即使我在模型上使用 AllowHtml 属性,我也会收到 HttpRequestValidationException
I get an HttpRequestValidationException even when I AllowHtml on the Model property
我允许 HTML 模型 属性 像这样:
型号
public class FooViewModel
{
[AllowHtml]
public string Description { get; set; }
}
查看
@using (Html.BeginForm("EditDescription", "Foo", FormMethod.Post))
{
@Html.AntiForgeryToken();
<input type="hidden" value="@(Model != null && Model.Item1 != null ? Model.Item1 : string.Empty)" name="fooId" />
<p>
<textarea name="Description" cols="100" id="Description">
@(Model != null && Model.Item2 != null ? Model.Item2 : string.Empty)
</textarea>
</p>
<p><input type="submit" value="Submit" /></p>
}
@section Scripts {
@Scripts.Render("~/bundles/nicEdit")
<script type="text/javascript">
bkLib.onDomLoaded(function()
{
new nicEditor({fullPanel : true}).panelInstance('Description');
});
</script>
}
控制器
public class FooController
{
public ActionResult EditDescription(string fooId)
{
if (Request.HttpMethod == "POST")
{
using (var context = new ApplicationDbContext())
{
var foo = context.Foos
.SingleOrDefault(f => f.Id == fooId);
// I get the HttpRequestValidationException here
foo.Description = Request["Description"];
context.SaveChanges();
return RedirectToAction("MyProfile");
}
}
}
}
我仍然收到请求验证异常。我什至尝试用 ValidateInput(false)
注释整个操作方法,因为视图只有一个字段,即模型上的单个 属性,但我仍然不断收到异常。
我清除了 ASP.NET 临时文件和文件夹缓存,清理并重建了我的解决方案,但都无济于事。
更新您的代码以使用未经验证的字符串版本,如下所示
foo.Description = Request.Unvalidated["Description"];
关于请求验证如何与 MVC 一起发展以及为什么输入验证属性不起作用的很好的读物
http://weblogs.asp.net/imranbaloch/understanding-request-validation-in-asp-net-mvc-3
我允许 HTML 模型 属性 像这样:
型号
public class FooViewModel
{
[AllowHtml]
public string Description { get; set; }
}
查看
@using (Html.BeginForm("EditDescription", "Foo", FormMethod.Post))
{
@Html.AntiForgeryToken();
<input type="hidden" value="@(Model != null && Model.Item1 != null ? Model.Item1 : string.Empty)" name="fooId" />
<p>
<textarea name="Description" cols="100" id="Description">
@(Model != null && Model.Item2 != null ? Model.Item2 : string.Empty)
</textarea>
</p>
<p><input type="submit" value="Submit" /></p>
}
@section Scripts {
@Scripts.Render("~/bundles/nicEdit")
<script type="text/javascript">
bkLib.onDomLoaded(function()
{
new nicEditor({fullPanel : true}).panelInstance('Description');
});
</script>
}
控制器
public class FooController
{
public ActionResult EditDescription(string fooId)
{
if (Request.HttpMethod == "POST")
{
using (var context = new ApplicationDbContext())
{
var foo = context.Foos
.SingleOrDefault(f => f.Id == fooId);
// I get the HttpRequestValidationException here
foo.Description = Request["Description"];
context.SaveChanges();
return RedirectToAction("MyProfile");
}
}
}
}
我仍然收到请求验证异常。我什至尝试用 ValidateInput(false)
注释整个操作方法,因为视图只有一个字段,即模型上的单个 属性,但我仍然不断收到异常。
我清除了 ASP.NET 临时文件和文件夹缓存,清理并重建了我的解决方案,但都无济于事。
更新您的代码以使用未经验证的字符串版本,如下所示
foo.Description = Request.Unvalidated["Description"];
关于请求验证如何与 MVC 一起发展以及为什么输入验证属性不起作用的很好的读物 http://weblogs.asp.net/imranbaloch/understanding-request-validation-in-asp-net-mvc-3