c# - 自定义标签助手不工作

标签 c# asp.net-core model asp.net-core-tag-helpers

我遵循了一些关于为 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/

相关文章:

c# - 在 Silverlight 中重用自定义样式

c# - 尝试注册时出现 Microsoft.AspNetCore.Identity.IdentityError

asp.net-core - 如何更改 Razor 组件中 HTML 文档头部的标题和其他 html 元素?

ruby-on-rails - 在 Rails 中创建新模型实例时发送电子邮件的最佳方式?

node.js - 一对一关联是否应该填充两侧?

C# 将字节数组 append 到现有文件

c# - EPPlus:如何设置合并单元格的样式?

python - Django:通过外键查找所有反向引用

c# - 为什么此 WCF 服务一次只能处理一个请求?

dependency-injection - asp.net 5依赖注入(inject)拦截调用方法