RazorLight: Cannot use HtmlHelper from view

Using @Html.Raw("<span>test</span>") returns a TemplateCompilationException. With internal error message “The name ‘Html’ does not exist in the current context”.

I assume you need to setup the HtmlHelper<TModel>() for it to work? Any other way i can pass inn a Html string from a model?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I will add html helpers support in the future, so you won’t see this annoying errors, but for now - you should deal with it)

Posting this as a community service, since this issue is the number one Google hit for “RazorLight raw” at the moment.

The solution is this:

@(new Microsoft.AspNetCore.Html.HtmlString(myHtmlString))

It’s mentioned in a comment of an issue that referenced this one in 2017: https://github.com/lukencode/FluentEmail/issues/78#issuecomment-349482849

This will compile fine in ASP.NET Core apps that also use RazorLight for whatever reason.

You’re welcome and enjoy your day. ❤️

I moved on to just using regular razor, it was not very difficult. Using it in my MIT licensed mail sender project.

https://github.com/DigitalRuby/MailDemon/blob/master/Services/RazorRenderer.cs https://github.com/DigitalRuby/MailDemon/blob/master/Services/ViewRenderer.cs

It’s been 2.5 years, still no html.raw capability. What is needed to add this?

RazorLight has no HtmlHelpers support. So for Raw encoding use our own iimplementation of this

@Raw("Enter your text here")

Yes, this works 👍 but it is a bit ugly since VS returns an error and red squiggly line. Anyway to tell VS that this is correct? a using statement?

Error CS0103 The name ‘Raw’ does not exist in the current context