自定义标签助手调试

Custom Tag helper debugging

我正在尝试测试我正在尝试创建的新标签助手。我无意中发现了新的 .net 核心验证的一个缺点,无法更改 class post 验证。所以如果我想给我的错误一个红色背景,跨度总是存在并且不会改变。所以,我决定制作自己的标签助手。问题是我似乎无法让它工作或触发。我什至不能让它达到一个断点。到目前为止,这是我对标签助手的了解。

namespace MusicianProject.TagHelpers
{
    // You may need to install the Microsoft.AspNetCore.Razor.Runtime package into your project
    [HtmlTargetElement("invalid-class",  Attributes = "validation-class")]
    public class ValidateClassTagHelper : TagHelper
    {
        public ValidateClassTagHelper(IHtmlGenerator generator) 
        {

        }
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {


            return base.ProcessAsync(context, output);
        }


        public override void Process(TagHelperContext context, TagHelperOutput output)
        {

            output.Attributes.Add("class", "test");
            var attr = context.AllAttributes;


        }
    }
}

这是我注册视图中的用法。

<div class="container">

    <form asp-controller="Account" asp-action="Register" method="post">

        <div class="col-md-4 col-md-offset-4">

            <div class="form-group">
                <label asp-for="FirstName"></label>
                <input class="form-control" type="text" asp-for="FirstName" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="LastName"></label>
                <input class="form-control" asp-for="LastName" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Email"></label>
                <input class="form-control" type="text" asp-for="Email" />
                <span validation-class="alert alert-danger" invalid-class="test" asp-validation-for="Email" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Password"></label>
                <input asp-for="Password" type="password" id="password" class="form-control" />
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="ConfirmPassword"></label>
                <input asp-for="ConfirmPassword" type="password" id="confirm-password" class="form-control" />
                <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
            </div>


            <div class="btn-group text-center">
                <button class="btn btn-default">Sign up!</button>
                <button class="btn btn-danger">Cancel</button>
            </div>
        </div>

    </form>
</div>

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

是的,我在我的 _ViewImports.cshtml 文件中注册了项目程序集名称。

@using MusicianProject
@using MusicianProject.Models
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
@addTagHelper "*, MusicianProject"

现在我不确定某些文件的放置是否重要,但我的 _ViewImports.cshtml 文件位于我的视图文件夹根目录 (src/Views/_ViewImports.cshtml) 中,并且我的标签助手有自己的根文件夹 (src/TagHelpers/*).

我错过了什么,我该如何更正?

如果我理解正确的话,你的问题是你的断点没有被击中。问题出在这里:

[HtmlTargetElement("invalid-class",  Attributes = "validation-class")]

HTMLTargetElement 的第一个参数是您要定位的标记,因此在您的情况下为 "span"。您在那里输入了 class 名称。

这样至少你的断点会被击中,我相信你会从那里找出其余的。

您有 2 个问题

1- HtmlTargetElement 是可以使用此标签助手的标签名称,例如:span、div、table ... 2-您没有在代码中使用标签助手,因此,它永远不会被触发。默认情况下,它不会应用于 HtmlTargetElement 中指定的所有标签,您应该通过将 validate-class 属性添加到 span 标签来调用它。

要了解有关标签助手的更多信息,请查看此 link https://blogs.msdn.microsoft.com/msgulfcommunity/2015/06/17/developing-custom-tag-helpers-in-asp-net-5/