6

I've been trying to add a custom tag helper, but when I try to add the assembly as a reference in the _ViewImports.cshtml it doesn't recognise it.

I'm doing this (_ViewImports.cshtml):

@using ProjectName.Web.Main
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, TagHelpers

and also I've tried:

@addTagHelper *, ProjectName.Web.Main.TagHelpers

my tag helper is the following:

using Microsoft.AspNetCore.Razor.TagHelpers;
namespace ProjectName.Web.Main.TagHelpers
{
    [HtmlTargetElement(Attributes="class")]
    public class CssClassTagHelper : TagHelper
    {
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.Attributes.Add(new TagHelperAttribute("class","testing"));

        }
    }
}

but when I run the app, I get the following exception:

Cannot resolve TagHelper containing assembly 'TagHelpers'. Error: Could not load file or assembly 'TagHelpers, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. @addTagHelper *, TagHelpers

I already have googled it but I'm getting mixed answers between the different dotnetcore releases, so far I haven't found any solution.

I'm using RC2.

any ideas, suggestions?

1 Answer 1

10

According to Asp.Net Core docs:

To expose all of the Tag Helpers in this project (which creates an assembly named AuthoringTagHelpers), you would use the following:

@using AuthoringTagHelpers 
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper "*, AuthoringTagHelpers"

In other words, second parameter of addTagHelper is an assembly name not a namespace.

So you should use ProjectName.Web.Main (I assumed that your project name is) in _ViewImports.cshtml :

@using ProjectName.Web.Main
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, ProjectName.Web.Main
Sign up to request clarification or add additional context in comments.

1 Comment

thanks dude! this solved it for me. such a small thing causing such an issue :-D SHot Man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.