问题
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?
回答1:
You need to provide only assembly name in the view imports file.
_ViewImports.cshtml:
@addTagHelper *, ToolConstrolSystem
回答2:
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.
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/48271514/custom-tag-helper-not-working