Custom tag helper not working

Matthew Goulart picture Matthew Goulart · Jan 15, 2018 · Viewed 10.6k times · Source

I followed a few guides on creating a custom tag helper for ASP Core.

This is my helper:

using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;

namespace ToolControlSystem.TagHelpers
{
    [HtmlTargetElement("description", Attributes = DescriptionAttributeName, TagStructure = TagStructure.NormalOrSelfClosing)]
    public class DescriptionTagHelper : TagHelper
    {
        private const string DescriptionAttributeName = "asp-for";


        [HtmlAttributeName(DescriptionAttributeName)]
        public ModelExpression Model { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            base.Process(context, output);

            var description = GetDescription(Model.ModelExplorer);

            output.TagName = "span";
            output.TagMode = TagMode.StartTagAndEndTag;
            output.Content.SetContent(description);
        }

        private string GetDescription(ModelExplorer modelExplorer)
        {
            string description;
            description = modelExplorer.Metadata.Placeholder;

            if (String.IsNullOrWhiteSpace(description))
            {
                description = modelExplorer.Metadata.Description;
            }

            return description;
        }
    }
}

I drop this in _ViewImports.cshtml: @addTagHelper *, ToolConstrolSystem.TagHelpers

Annnndd... nothing. No intellisense, no tag replacing...

Any ideas?

Answer

Anuraj picture Anuraj · Jan 16, 2018

You need to provide only assembly name in the view imports file.

_ViewImports.cshtml:

@addTagHelper *, ToolConstrolSystem