我有一个包含属性Content的Model,其中包含HTML字符串。
var model = new { Content = ... }
Razor.Parse(templateBody, model)
如何使用独立Razor渲染此字符串。
我试过了:
@(new HtmlString(Model.Content))
以及
@(HttpUtility.HtmlDecode(Model.Content))
Model.Content渲染始终是HTML转义。
答案 0 :(得分:42)
这应该有效Html.Raw(Model.Content)
答案 1 :(得分:16)
在RazorEngine库中,特殊的“do-not-escape-me”类型为RazorEngine.Text.IEncodedString
。在模板基类上使用简单的辅助方法:
@Raw("<script>alert('!');</script>")
要说服VS和Resharper为您提供IntelliSense,请在模板顶部添加以下行:
@inherits RazorEngine.Templating.TemplateBase