How do I output raw html when using RazorEngine (NOT from MVC)

KallDrexx picture KallDrexx · Mar 12, 2012 · Viewed 61.1k times · Source

I am trying to generate emails with HTML content. this content has already gone through sanitation so I am not worried in that regard, however when I call:

Razor.Parse(template, model);

on the following Razor template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <body>
        @(new System.Web.HtmlString(Model.EmailContent))
    </body>
</html>

the email that is outputted is HTMl encoded, but I need it decoded. How can I accomplish this?

Answer

Matthew Abbott picture Matthew Abbott · Mar 12, 2012

RazorEngine, like MVC's Razor View Engine, will automatically encode values written to the template. To get around this, we've introduce an interface called IEncodedString, with the default implementations being HtmlEncodedString and RawString.

To use the latter, simply make a call to the inbuilt Raw method of TemplateBase:

@Raw(Model.EmailContent)