Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom tag helpers

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?

like image 476
pedrommuller Avatar asked Jun 18 '16 17:06

pedrommuller


1 Answers

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
like image 86
adem caglin Avatar answered Sep 22 '22 06:09

adem caglin