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?
See Managing Tag Helper scope
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.