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:32

Another thing to check if you're finding you can't set your content with calls like:

output.Content.AppendHtml("<tags...>");
output.Content.SetHtmlContent("<tags...>");

Is to check that you didn't use your tag helper as an empty/void element, e.g.:

<my-tag-helper attr="value" />

To add content the tag helper must have a closing tag:

<my-tag-helper attr="value"></my-tag-helper>
查看更多
相关推荐>>
3楼-- · 2020-05-24 20:41

And also keep in mind that at the moment (March 2020) .Net Core 3 automatically generates the namespaces with underscores in it. Nevertheless, the assembly name will be exactly the same as the folder name (even if it does contain whitespaces and other uncommon for folder name symbols). It can cause troubles with adding your custom tag helpers.

Let's assume:

  1. You have a folder called SUPER-TEST
  2. You cd into it and call dotnet new mvc
  3. This makes your new project have a namespace "SUPER_TEST".
  4. You create a tag helper in this namespace and include the assembly name into the _ViewImports like this:
***
@addTagHelper *, SUPER_TEST
***

It's not gonna work. Because in fact your assembly is now called SUPER-TEST. .Net Core runtime replaces underscores with dashes when creating the new project.

So, you have to import the tag helpers from SUPER-TEST, like this:

***
@addTagHelper *, SUPER-TEST
***
查看更多
Luminary・发光体
4楼-- · 2020-05-24 20:42

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

_ViewImports.cshtml: 

@addTagHelper *, ToolConstrolSystem
查看更多
孤傲高冷的网名
5楼-- · 2020-05-24 20:42

A way you can make the search for your TagHelpers a bit quicker is to provide the namespace that your tag helpers live in instead of the asterisk, which will look everywhere in your assembly.

@addTagHelper com.excellor.TagHelpers.*, excellor

Where excellor is the assemblyname and com.excellor.TagHelpers is the namespace the tag helpers reside in.

Note, the following entry is NOT needed as long as you are namespacing your tag helpers.

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
查看更多
来,给爷笑一个
6楼-- · 2020-05-24 20:48

So I wasted a bit of time chasing the problem where a taghelper was not working. After a while I went to bed and today with fresh eyes I gave it another try, was then that I realized that I did not add the @addTagHelper in the Razor Pages folder only in the Views folder.

So if you like I have this /Pages + /Views concepts make sure to go through all those _ViewImports.cshtml. Leaving this note here hopefully will remind someelse's tired brain, and if it helped take a break and go for a walk or a snooze.

enter image description here

查看更多
霸刀☆藐视天下
7楼-- · 2020-05-24 20:48

I had the same problem, but it was the result of a rouge, accidental semi-colon at the end of the @addTagHelper line.

I had:

@addTagHelper *, ToolConstrolSystem;

Instead of:

@addTagHelper *, ToolConstrolSystem

While the solution will build and run fine, the tag helper will not work if the @addTagHelper line contains a semi-colon.

查看更多
登录 后发表回答