How to prevent escaping html in Razor (standalone)

2020-04-01 22:55发布

I have a Model with property Content which contains HTML string.

var model = new { Content = ... }
Razor.Parse(templateBody, model)

How can I render this string using standalone Razor.

I tried:

@(new HtmlString(Model.Content))

and also

@(HttpUtility.HtmlDecode(Model.Content))

Model.Content renders always HTML-escaped.

2条回答
贪生不怕死
2楼-- · 2020-04-01 23:25

In RazorEngine library special "do-not-escape-me" type is RazorEngine.Text.IEncodedString. Use simple helper method on template base class:

 @Raw("<script>alert('!');</script>")

To convince VS and Resharper to give you IntelliSense add following line at the top of template:

@inherits RazorEngine.Templating.TemplateBase
查看更多
SAY GOODBYE
3楼-- · 2020-04-01 23:39

this should work Html.Raw(Model.Content)

查看更多
登录 后发表回答