SteinTech
SteinTech

Reputation: 4068

TagHelpers doesn't get invoked after moving them to a different project

I'm having some issues with asp.net core not finding and invoking my tag helpers. After I moved the taghelpers from a seperate assembly (LC.Tools.Utility.TagHelpers) to LC.Tools.Utility it doesnt seem like they get discovered even though the namespace is the same as before.

Sample cshtml:

<lc:css src="styles.min.css" />
<lc:css src="styles.bootstrap.min.css" />
<lc:css src="styles._layout.min.css" />

When looking at the source for the page the tags are exactly the same.

_ViewImports.cshtml:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers 
@addTagHelper *, LC.Smokers.Albino.TagHelpers
@addTagHelper *, LC.Tools.Utility.TagHelpers

LC.Tools.Utility.TagHelpers.IncludeCssTagHelper.cs (in assembly LC.Tools.Utility):

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace LC.Tools.Utility.TagHelpers
{
    [HtmlTargetElement("lc:css", Attributes = "src", TagStructure = TagStructure.WithoutEndTag)]
    public class IncludeCssTagHelper : Base
    {
        public IncludeCssTagHelper(IHostingEnvironment env) : base(env) { }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "link";
            output.TagMode = TagMode.SelfClosing;

            bool isDebug = this.Environment.IsDevelopment() && this.DebugSrc.Length > 0;
            string path = "/CSS/";

            if (isDebug)
            {
                path += this.DebugSrc;

                if (Utility.IsUrl(this.DebugSrc))
                {
                    path = this.DebugSrc;
                }
            }
            else if (!string.IsNullOrEmpty(this.Src))
            {
                path += this.Src;

                if (Utility.IsUrl(this.Src))
                {
                    path = this.Src;
                }
            }

            output.Attributes.Clear();

            output.Attributes.SetAttribute("href", path.ToLower());
            output.Attributes.SetAttribute("rel", "stylesheet");
            output.Attributes.SetAttribute("type", "text/css");

            base.Process(context, output);
        }

        [HtmlAttributeName("src")]
        public string Src { get; set; }

        [HtmlAttributeName("debug-src")]
        public string DebugSrc { get; set; } = string.Empty;
    }
}

I've tried deleting the bin and obj folders from all the projects and rebuild the entire solution and removed and added reference to the project.

Any ideas?

Upvotes: 0

Views: 461

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93083

The addTagHelper directive expects two comma-separated parameters. The docs has this to say about how it works:

The first parameter after @addTagHelper specifies the Tag Helpers to load (we are using "*" for all Tag Helpers), and the second parameter "Microsoft.AspNetCore.Mvc.TagHelpers" specifies the assembly containing the Tag Helpers.

Note what I've highlighted in bold: It's not the namespace that is needed; it's the assembly. In your example, you need to change that second parameter to reflect the new hosting assembly:

@addTagHelper *, LC.Tools.Utility

Upvotes: 2

Related Questions