Search code examples
c#asp.net-mvcasp.net-coreasp.net-core-tag-helpers

How to use TagHelpers in asp.net core 3.0?


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?


Solution

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

    @addTagHelper *, MyProject