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?
FYI I have a fork that includes the @Html.Raw(...) syntax here:
https://github.com/Antaris/RazorEngine/pull/105
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 beingHtmlEncodedString
andRawString
.To use the latter, simply make a call to the inbuilt
Raw
method ofTemplateBase
:If you have a custom base class for your templates, you can code
Write
method to behave similar to normal MVC template: if the output value isIHtmlString
it should not encode it.Here's the code I'm using in my
TemplateBase
class:I found all of these worked with me.
Built a wrapper for RazorEngine that adds in support for
@Html.Raw()
and@Html.Partial()
https://github.com/b9chris/RazorEngineComplete
I am using RazorEngine 3.8.2 and
@Raw(Model.Content)
is working perfectly fine for me.