Search code examples
c#.net.net-coretag-helpersasp.net-core-tag-helpers

Custom TagHelper does not work in a ASP.NET Web Project (.Net 5)


I have a custom TagHelper like described in the Docs: A minimal Tag Helper

using Microsoft.AspNetCore.Razor.TagHelpers;

namespace MyTagHelpers.TagHelpers
{
    public class EmailTagHelper : TagHelper
    {
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "a";    // Replaces <email> with <a> tag
        }
    }
}

I add my EmailTagHelper in _ViewImports.cshtml:

@addTagHelper *, MyTagHelpers

And use it on the Razor Page

<email></email>

But it does not work.


Solution

  • My Project has Assembly Name set to MyProject.

    File MyProject.csproj

     <PropertyGroup>
            <AssemblyName>MyProject</AssemblyName>
     </PropertyGroup>
    

    So, the generated assembly will have file name MyProject.dll

    To make my EmailTagHelper work I need to add it with the right Assembly Name:

    @addTagHelper *, MyProject
    

    It works, despite the parent namespace of my EmailTagHelper is still the same MyTagHelpers.