Skip to content Skip to sidebar Skip to footer

How Do I Output Raw Html When Using Razorengine (not From Mvc)

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, m

Solution 1:

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)

Solution 2:

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

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

Solution 3:

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

Solution 4:

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"publicvirtualvoidWrite(objectvalue)
{
    if (valueis IHtmlString)
        WriteLiteral(value);
    else
        WriteLiteral(AntiXssEncoder.HtmlEncode(value.ToString(), false));
}

// Writes literals like markup: "<p>Foo</p>"publicvirtualvoidWriteLiteral(objectvalue)
{
    Buffer.Append(value);
}

Solution 5:

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

https://github.com/b9chris/RazorEngineComplete

Post a Comment for "How Do I Output Raw Html When Using Razorengine (not From Mvc)"