简体   繁体   English

标签助手未在 ASP.NET Core 2 中处理

[英]Tag helper not being processed in ASP.NET Core 2

I've added the following tag helper:我添加了以下标签助手:

using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace X.TagHelpers
{
    [HtmlTargetElement(Attributes = ValidationForAttributeName + "," + ValidationErrorClassName)]
    public class ValidationClassTagHelper : TagHelper
    {
        private const string ValidationForAttributeName = "k-validation-for";
        private const string ValidationErrorClassName = "k-error-class";

        [HtmlAttributeName(ValidationForAttributeName)]
        public ModelExpression For { get; set; }

        [HtmlAttributeName(ValidationErrorClassName)]
        public string ValidationErrorClass { get; set; }

        [HtmlAttributeNotBound]
        [ViewContext]
        public ViewContext ViewContext { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            Console.WriteLine("\n\n------------!!!!!!---------\n\n");
            ModelStateEntry entry;
            ViewContext.ViewData.ModelState.TryGetValue(For.Name, out entry);
            if (entry == null || !entry.Errors.Any()) return;
            var tagBuilder = new TagBuilder(context.TagName);
            tagBuilder.AddCssClass(ValidationErrorClass);
            output.MergeAttributes(tagBuilder);
        }
    }
}

and then in _ViewImports.cshtml I've added the line:然后在_ViewImports.cshtml我添加了以下行:

@addTagHelper *, X.TagHelpers

The file is compiled correctly and if I introduce a syntax error dotnet build warns me about it.该文件已正确编译,如果我引入语法错误dotnet build警告我。

Then in one of my pages I add:然后在我的其中一个页面中添加:

<div k-validation-for="OldPassword" k-error-class="has-danger"></div>

If I load the page I see no console output on the server side and the k-validation-for and k-error-class are forwarded to the generated page as is (as opposed to adding the has-danger class to the class attribute).如果我加载页面,我在服务器端看不到控制台输出,并且k-validation-fork-error-class将按原样转发到生成的页面(而不是将has-danger类添加到class属性) .

What am I doing wrong?我究竟做错了什么?

When registering Tag Helpers, it's the assembly that is required, not the namespace - explained in the docs .注册 Tag Helpers 时,需要的是程序集,而不是命名空间 - 在docs中进行了解释。

...the second parameter "Microsoft.AspNetCore.Mvc.TagHelpers" specifies the assembly containing the Tag Helpers. ...第二个参数“Microsoft.AspNetCore.Mvc.TagHelpers”指定包含标记帮助程序的程序集。 Microsoft.AspNetCore.Mvc.TagHelpers is the assembly for the built-in ASP.NET Core Tag Helpers. Microsoft.AspNetCore.Mvc.TagHelpers 是内置 ASP.NET Core 标记帮助程序的程序集。

So in your case, you can just change this:所以在你的情况下,你可以改变这个:

@addTagHelper *, X.TagHelpers

To this:对此:

@addTagHelper *, X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM