Stephan
Stephan

Reputation: 1999

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.

Question:

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

Upvotes: 1

Views: 1859

Answers (3)

Rawson
Rawson

Reputation: 234

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

Upvotes: 2

Jeremy Lakeman
Jeremy Lakeman

Reputation: 11090

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

@addTagHelper *, MyProject

Upvotes: 1

user12376840
user12376840

Reputation: 13

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>

Upvotes: 0

Related Questions