Custom tag helper not working

2020-05-24 20:20发布

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?

7条回答
再贱就再见
2楼-- · 2020-05-24 20:56

See Managing Tag Helper scope

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

The code above uses the wildcard syntax ("*") to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.

查看更多
登录 后发表回答