5

我在 C# MVC4 项目中有这个类:

public class SaveModel
{
    ....

    [AllowHtml]
    public string BodyHtml { get; set; }
    [AllowHtml]
    public Dictionary<string, string> AdditionalTemplate { get; set; }
}

一个看起来像这样的控制器动作

public ActionResult SaveTemplate(SaveModel model)
{  
    ....
}

BodyHtml 工作正常,但由于某种原因 AllowHtml 在字典上不起作用,我收到如下错误:

A potentially dangerous Request.Form value was detected from 
the client (additionalTemplate[0].value="<tr>..."

除了通过将 [ValidateInput(false)] 放在我的操作上来禁用整个请求的验证之外,有什么办法可以绕过它?

[ValidateInput(false)]
public ActionResult SaveTemplate(SaveModel model)
{  
    ....
}
4

1 回答 1

2

作为快速解决方法,您可以为键值集合创建自己的类型,其中将是两个属性。值属性可以标记为 AllowHtml,如:

    public List<MyCustomItem> AdditionalTemplate { get; set; }  

废话

class MyCustomItem
    {
      public string Key { get; set; }
      [AllowHtml]
      public string Value { get; set; }
    }
于 2016-09-19T13:39:22.037 回答