31

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.

Exta
  • 884
  • 2
  • 8
  • 14

2 Answers2

65

this should work Html.Raw(Model.Content)

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
19

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
Artem Tikhomirov
  • 21,497
  • 10
  • 48
  • 68