标记助手在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);
}
}
}
,然后在 _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
被原样转发到生成的页面(与添加具有危险级别
到 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).
我是什么
推荐答案
注册标签助手时,需要的是 assembly ,而不是名称空间-在 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标记帮助器的程序集。
...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中未得到处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!