0
votes

I'm trying to make a custom tag helper work in asp-net core 3.0.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace MyProject.TagHelpers
{
    [HtmlTargetElement("p", Attributes = "markdown")]
    [HtmlTargetElement("markdown")]
    [OutputElementHint("p")]
    public class MarkdownTagHelper : TagHelper
    {
        public async override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            output.Content.SetHtmlContent("<p>lkajsdlkjasdlkjasd</p>");
        }
    }
}

_ViewImports.cshtml:

@using MyProject
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, MyProject

I'm trying to reference <markdown></markdown> in the TermsConditions.cshtml file. (Full file:)

<div>
    <markdown></markdown>
</div>

enter image description here

But still, the markdown tag is never replaced when calling that view.

I found many questions, blogs, ..., but nothing worked so far. I checked for the following common mistakes.

  • Tag helper class is made public
  • _ViewImports.cshtml is placed in Views folder where all the other views reside
  • Tried the async and non-async version of Process

Question:

What do I have to do in order to make TagHelpers work?

3
It looks like you're doing everything right, assuming that the assembly with the tag helper is called FeatureNinjas?Jeremy Lakeman
@JeremyLakeman, yes, just updated the post to be correct. MyProject is the namespace. Mp.Web is the assembly name.Stephan
@JeremyLakeman, based on your comment, I also added @addTagHelper *, Mp.Web to _ViewImports.cshtml, and now it works. So you really have to use the assembly name here, not the namespace. It is written in the docs, but I somehow missed this. Thanks! (Just post this as an answer and I will accept it).Stephan

3 Answers

0
votes

Everything looks right, from the available information. So I suspect that this isn't actually your assembly name;

@addTagHelper *, MyProject
0
votes

Doesn't look like you are applying it correctly..

Change

[HtmlTargetElement("p", Attributes = "markdown")]
[HtmlTargetElement("markdown")]
[OutputElementHint("p")]

To

[HtmlTargetElement("markdown")]

In your original code, you would need to do something like <p markdown="some text"></p>

0
votes

Maybe I'll save someone time.. In my case the AssemblyName was missing in the current project, so the tag helpers was not applied.

In _ViewImports.cshtml file should be used this AssemblyName like:

@using AssemblyName
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, AssemblyName

And don't forget to add these assemblies:

Microsoft.AspNetCore.Mvc.Razor
Microsoft.AspNetCore.Mvc.TagHelpers
Microsoft.AspNetCore.Razor.Runtime