MVC4 AllowHtml无法与Dictionary<;字符串,字符串>;

本文关键字:字符串 lt gt Dictionary AllowHtml MVC4 | 更新日期: 2023-09-27 18:25:46

我在一个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在Dictionary上不起作用,我收到了这样的错误:

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

除了通过在我的操作中输入[ValidateInput(false)]来禁用整个请求的验证之外,还有什么方法可以绕过它吗?

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

MVC4 AllowHtml无法与Dictionary<;字符串,字符串>;

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

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

blabla

class MyCustomItem
    {
      public string Key { get; set; }
      [AllowHtml]
      public string Value { get; set; }
    }