Rendering HTML content with @Html.Raw in Razor Mail Template
When doing e-mails, I use the RazorEngineService in RazorEngine.Templating, e.g. in my case, it looks like this:
using RazorEngine.Templating;
RazorEngineService.Create().RunCompile(html, ...)
Assuming you are using the same assembly, @Html.Raw does NOT exist with this usage. I was finally able to get raw HTML output by doing this in my e-mails:
@using RazorEngine.Text
@(new RawString(Model.Variable))
Razor tag inside Html.Raw(...)
You may have some architectural issues that lead you to have some razor code on your Model property.
Anyway, you can do it by using some external libs such RazorTemplates.
Here is a sample :
var template = Template.Compile(Model.myRawHtmlContainingRazorTags);
@Html.Raw(template.Render(Model));
Inserting html using the RazorEngine to be rendered as HTML is populated template
Model.URL
isn't a string. It's probably a Newtonsoft.Json.Linq.JValue
. You'll need to convert it to a string first using:
@Raw(Model.URL.ToString())
Just be careful and ensure Model.URL isn't null.
Generate html in body with RazorEngine email template
UPDATE:
The solution has been found here: https://github.com/Antaris/RazorEngine/issues/34
And here: RazorEngine: cannot use Html.Raw
It's enough to use @(new RawString("html string here")) or @Raw("html
string here") instead of @Html.Raw("html string here").
Render Html code with razor
You should use:
@Html.Raw(HttpUtility.HtmlDecode(inputText))
Decode and then render in html
How do I output raw html when using RazorEngine (NOT from MVC)
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)
Store a path with tilde (~) and display it with html.raw in razor
You can use the Url.Content method like this:
var markup = $@"<img src=""{Url.Content("~/images/image.png")}"">";
Or, if you prefer to use string.Format
instead of $
:
var markup = string.Format(@"<img src=""{0}"">", Url.Content("~/images/image.png"));
That will turn "~/images/image.png" into an application absolute path so the actual output will be something like
<img src="/images/image.png">
Related Topics
How to Switch Two Divs in HTML
Targeting Flex Items on the Last or Specific Row
Css Overflow-X: Visible; and Overflow-Y: Hidden; Causing Scrollbar Issue
My Inline-Block Elements Are Not Lining Up Properly
Why Is the ≪Center≫ Tag Deprecated in Html
Vertically Align an Image Inside a Div With Responsive Height
Html/Javascript One Click Print (No Dialogs)
Mystery White Space Underneath Image Tag
Setting the Width of Inline Elements
How to Use Two Ngfor in HTML Angular 4
Why Is Percentage Height Not Working on My Div
Shape With a Slanted Side (Responsive)
≪Button≫ Vs. ≪Input Type="Button" /≫. Which to Use
Why Doesn't Percentage Padding/Margin Work on Flex Items in Firefox and Edge
Binding Select Element to Object in Angular