我遵循了一些关于为 ASP Core 创建自定义标签助手的指南。
这是我的 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;
}
}
}
我将其放入 _ViewImports.cshtml
:@addTagHelper *, ToolConstrolSystem.TagHelpers
Annnndd...没什么。没有智能感知,没有标签替换...
有什么想法吗?
最佳答案
您只需在 View 导入文件中提供程序集名称。
_ViewImports.cshtml:
@addTagHelper *, ToolConstrolSystem
关于c# - 自定义标签助手不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48271514/