gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:21:21 26 4
gpt4 key购买 nike

我添加了以下标签助手:

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 中添加了以下行:

@addTagHelper *, X.TagHelpers

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

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

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

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

我做错了什么?

最佳答案

注册 Tag Helper 时,需要的是程序集,而不是 namespace - 在 docs 中解释.

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

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

@addTagHelper *, X.TagHelpers

对此:

@addTagHelper *, X

关于c# - 标签助手未在 ASP.NET Core 2 中处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com