【问题标题】:Custom Tag helper debugging自定义标签助手调试
【发布时间】:2017-04-29 07:07:34
【问题描述】:

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

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/*)。

我遗漏了什么,我该如何纠正?

【问题讨论】:

    标签: c# razor asp.net-core asp.net-core-mvc tag-helpers


    【解决方案1】:

    您有 2 个问题

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

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

    【讨论】:

      【解决方案2】:

      如果我理解正确,您的问题是您的断点没有被命中。问题就在这里:

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

      HTMLTargetElement 的第一个参数是您要定位的标签,因此在您的情况下是“span”。你在那里输入了一个类名。

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

      【讨论】:

        猜你喜欢
        • 2019-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        相关资源
        最近更新 更多