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

Active route custom TagHelper is not rendered


I have created a new tag helper (ActiveRouteTagHelper) with the attribute 'is-active-route'. However, when using this attribute on any Razor page, the helper process is never called. The application is running on .NET Core 2.2.

I have ensured that the @addTagHelper reference has been added to _ViewImports and that the class and override are both public.

_ViewImports.cshtml

@using SIRS_Web
@using SIRS_Web.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, SIRS_Web

ActiveRouteTagHelper.cs

namespace SIRS_Web.TagHelpers
{
    [HtmlTargetElement(Attributes = "is-active-route")]
    public class ActiveRouteTagHelper : TagHelper
    {
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.Attributes.RemoveAll("is-active-route");
        }
    }

_Layout.cshtml

    ...
    <a is-active-route href="#">Link</a>
    ...

I have added a breakpoint to the first line of the process override but this is never reached and the attribute is-active-route appears in the page.


Solution

  • @addTagHelper directive does not take a namespace it takes an assembly name. That would be your issue.

    Please take aside time to read the following link:

    https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring?view=aspnetcore-2.2#a-minimal-tag-helper