Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
KallDrexx Avatar asked Mar 12 '12 02:03

KallDrexx


People also ask

Can we use HTML Raw?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. In this article I will explain with an example, how to use Html.

What symbol is used by the razor engine in MVC to automatically encode HTML output?

You add code to a page using the @ character When you display content in a page using the @ character, as in the preceding examples, ASP.NET HTML-encodes the output.

Why not use HTML Raw?

Raw can result in a XSS vulnerability being exploitable since an attacker can craft a special URL containing a malicious JavaScript payload that will be executed by the victim's browser if he or she sends an invalid 2FA confirmation code.

What does HTML raw do C#?

Raw allows you to output text containing html elements to the client, and have them still be rendered as such. Should be used with caution, as it exposes you to cross site scripting vulnerabilities.


2 Answers

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) 
like image 167
Matthew Abbott Avatar answered Oct 06 '22 08:10

Matthew Abbott


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

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

like image 37
Tod Thomson Avatar answered Oct 06 '22 10:10

Tod Thomson