如何prevent在剃刀(独立的)逃跑的html?剃刀、独立、prevent、html

2023-09-05 00:05:44 作者:情话腻耳

我有一个属性内容,其中包含HTML字符串模型。

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.

我想:

@(new HtmlString(Model.Content))

和也

@(HttpUtility.HtmlDecode(Model.Content))

Model.Content始终呈现HTML转义。

Model.Content renders always HTML-escaped.

推荐答案

在RazorEngine库中特殊的拒收逃生我的类型是 RazorEngine.Text.IEn codedString 。使用简单的辅助方法对模板的基类:

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>")

要说服VS和ReSharper的给你智能感知在模板的顶部添加以下行:

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

@inherits RazorEngine.Templating.TemplateBase