0
votes

I have an Core Class Library containing custom Tag Helpers. I want to add this as a reference in a Core Web Application.

I right click on the project in Visual Studio, selects Add Reference and browses to the folder and choose the dll.

But when I try to use @addTagHelper I get:

Cannot resolve TagHelper containing assembly 'LC_PUBLIC_CORE'. Error: Could not load file or assembly 'LC_PUBLIC_CORE, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

My code:

LC_Public (Class Library)

namespace LC_PUBLIC_CORE.TagHelpers
{
    [HtmlTargetElement("LC_meta")]
    public class MetaTagHelper : TagHelper
    {
        private IHostingEnvironment _env;

        [HtmlAttributeName("filename")]
        public string Filename { get; set; } = "default.txt";

        public MetaTagHelper(IHostingEnvironment env)
        {
            _env = env;
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.Content.SetContent(System.IO.File.ReadAllText(System.IO.Path.Combine(_env.WebRootPath, "META", this.Filename)));
        }
    }
}

ASP_NET_CORE_Standard (Web Application) - _ViewImports.cshtml

@using ASP_NET_CORE_Standard
@using LC_PUBLIC_CORE
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, LC_PUBLIC_CORE

Do I need to add any additional code?

1
Your last @addTagHelper line is using a different (and obsolete) format than the 2nd last one. Also the wild card is used completely different. First parameter of @addTagHelper is for the tag helper class name (* for all), the second is the namespace where the tag helpers are located - Tseng
@tseng I've changed the format, but still same error. If I use LC_PUBLIC_CORE.TagHelpers I get "Cannot resolve TagHelper containing assembly 'LC_PUBLIC_CORE.TagHelpers'", Defining Copy Local = true on the reference also didn't do anything - SteinTheRuler
Did you rebuilt your solution after changing it? Some stuff doesn't get picked up immediately (namely EF core model changes and certain Razor aspects, i.e. for scaffolding) - Tseng
Yes @tsemg I tried that. But what I think is strange is that the "@using" statement looks like it finds the assembly (doesn't display an error in Visual Studio). - SteinTheRuler

1 Answers

0
votes

I got it working by creating a new Solution and added both Projects to the solution. Then added a reference to the LC_PUBLIC_CORE project in ASP_NET_CORE_Standard.

_ViewImports.cshtml:

@using ASP_NET_CORE_Standard
@using LC_PUBLIC_CORE
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, LC_PUBLIC_CORE