RazorLight: TagHelpers are not supported

Hi,

I would like to use your library for email-templates, which contains links to the application itself. Sadly, RazorLight seems to parse razor code, but not the ASP.NET Core TagHelpers. For example, the asp-controller and asp-action attribute to generate an action-controller link like this:

<a asp-controller="Account" asp-action="Login">Test</a>

Using dependency injection, a instance of IRazorLightEngineis injected in my constructor. There I call the Parsemethod like this:

var html = razorEngineLight.Parse("Shared/Test.cshtml", new object());

But in the rendered html, I don’t get a parsed link like

<a href="/Account/Login">Test</a>

as expected. Instead, I get 1:1 the source code with asp-controller and action attribute like showed above. So it seems that the _ViewImports.cshtml file in the View-folder is not globally parsed like ASP.NET Core do it. I tried to add the tag helpers manually using

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

in the Razor-View, but its not working. As a workaround, I tried to use the old UrlHelpers from ASP.NET like this way:

@{
    var link = Url.Action("Login", "Account", null);
}

<a href="@link">Testlink</a>

This is valid code, as it works in a normal Razor-View. But using RazorLight, I got a RazorLight.TemplateCompilationException: Failed to compile generated razor view. See CompilationErrors for detailed information. In the details, Visual Studio show me that there is one CompilationError (Cound = 1) but there seems no way to extend or otherwise view them.

As a result, it seems not possible to create action-controller links using Razor, which is a problem in my point of view. Would be pleased if this could be fixed.

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 32 (10 by maintainers)

Most upvoted comments

Still missing for years. I realize this is a free and open source project, but if anyone can give guidance on how to add html helpers to this project so that views work as they do in a vs project I’d be happy to do the work.

Hi there

As mentioned here #16 - taghelpers and UrlHelpers are not implemented. I am going to roll up a stable version soon, and more likely it will not be implemented there, as we are on Release Candidate now. I will add this to backlog, but expect this on the next major release.

In your case you can write pure HTML for building links for now, it’s not critical, but yes, a lot of people were asking where are tag and url helpers, so it has to be added.

Thanks, stay tuned

I found a better workaround.

@{    var rawString = new RazorLight.Text.RawString(content);   }
...
Here I want to print @(rawString) without any compile issues.

I think it would make sense to create a RazorLight static that can be injected with these helper functions, rather than just say “oh well.”

ditto: Any news about support for taghelpers and UrlHelpers? Missing support is a showstopper for me. And HtmlHelpers?

Badly missing functionality. I have to duplicate lots of views only because of this 😦 Anyway, big plus to the contributors for their good work on this promising package!

Any news about support for taghelpers and UrlHelpers? Missing support is a showstopper for me.

any news on adding this feature?? tag helper is base feature of razor views in asp.net core this is very useful

Which is the supported tags overview right now?

don’t work.

This will be implemented as a part of RazorLight 2.0 that will start it’s development at the moment .NET Core 2.0 will be released

@kudoz83 You can use @Raw in order to workaround Html.Raw(), which basically the same function but outside of HtmlHelper implemented as Razor page function