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

编程入门 行业动态 更新时间:2024-10-09 18:22:18
本文介绍了标签助手未在 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(" ------------!!!!!!--------- "); 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); } } }

然后在 _ViewImports.cshtml 中添加了以下行:

and then in _ViewImports.cshtml I've added the line:

@addTagHelper *, X.TagHelpers

文件已正确编译,如果我引入语法错误dotnet build 会警告我.

The file is compiled correctly and if I introduce a syntax error dotnet build warns me about it.

然后在我的其中一个页面中添加:

Then in one of my pages I add:

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

如果我加载页面,我在服务器端看不到控制台输出,k-validation-for 和 k-error-class 被转发到生成的页面原样(与将 has-danger 类添加到 class 属性相反).

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).

我做错了什么?

推荐答案

在注册标签助手时,需要的是程序集,而不是命名空间——在 docs.

When registering Tag Helpers, it’s the assembly that is required, not the namespace - explained in the docs.

...第二个参数Microsoft.AspNetCore.Mvc.TagHelpers"指定包含标记帮助程序的程序集.Microsoft.AspNetCore.Mvc.TagHelpers 是内置 ASP.NET Core Tag Helpers 的程序集.

...the second parameter "Microsoft.AspNetCore.Mvc.TagHelpers" specifies the assembly containing the Tag Helpers. Microsoft.AspNetCore.Mvc.TagHelpers is the assembly for the built-in ASP.NET Core Tag Helpers.

所以在你的情况下,你可以改变这个:

So in your case, you can just change this:

@addTagHelper *, X.TagHelpers

为此:

@addTagHelper *, X

更多推荐

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

本文发布于:2023-11-03 05:12:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:助手   标签   ASP   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!