118

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?

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • I answered this, but it was deleted as a duplicate answer because I answered it here: http://stackoverflow.com/questions/23603593/razorengine-cannot-use-html-raw ... this answer works for both MVC and RazorEngine. – Brian Rice Oct 23 '15 at 02:49

5 Answers5

196

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)
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • 4
    @Matthew Sorry for reviving, but even though this compiles, it does not look pretty in the VS HTML Editor, it wont recognize Model or Raw anymore. Any workarounds? – Jeff Apr 04 '13 at 12:00
  • 5
    @Jeff I'm thinking the right approach is for RazorEngine to expose a static class Html.cs that has a static method .Raw so that it will process standard calls to Html.Raw that can be used to get correct syntax highlighting in VS Razor editor. – Tod Thomson May 13 '13 at 07:09
  • 9
    @Manfred @Raw() works fine in RazorEngine (not ASP.NET MVC's Razor ;) ) – Jeff Jun 02 '13 at 15:35
  • Would like to see this solved too. This causes the whole project not to build with MvcBuildViews enabled... – guidupuy Oct 22 '13 at 10:07
  • 2
    The "HtmlEncodedString" doesnt work! Instead use: RawString. See an example that works! public RazorEngine.Text.IEncodedString GetMarcaFinalDocumento() { return new RazorEngine.Text.RawString("
    Hi there!
    ");}
    – Charles Dec 02 '14 at 10:28
17

FYI I have a fork that includes the @Html.Raw(...) syntax here:

https://github.com/Antaris/RazorEngine/pull/105

Tod Thomson
  • 4,773
  • 2
  • 33
  • 33
  • 4
    @Html.Raw() required no upfront configuration. Good solution. – MartinJH Mar 21 '17 at 10:41
  • Me too. I was able to generated HTML tags and values in Razor Code and pass it to View with ViewData and display using @Html.Ras(ViewData["NextEvent"] – JustJohn Jun 22 '20 at 00:39
5

I am using RazorEngine 3.8.2 and @Raw(Model.Content) is working perfectly fine for me.

curious.netter
  • 774
  • 10
  • 16
3

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 is IHtmlString it should not encode it.

Here's the code I'm using in my TemplateBase class:

// Writes the results of expressions like: "@foo.Bar"
public virtual void Write(object value)
{
    if (value is IHtmlString)
        WriteLiteral(value);
    else
        WriteLiteral(AntiXssEncoder.HtmlEncode(value.ToString(), false));
}

// Writes literals like markup: "<p>Foo</p>"
public virtual void WriteLiteral(object value)
{
    Buffer.Append(value);
}
Iravanchi
  • 5,139
  • 9
  • 40
  • 56
1

Built a wrapper for RazorEngine that adds in support for @Html.Raw() and @Html.Partial()

https://github.com/b9chris/RazorEngineComplete

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190