自定义标记帮助程序调试

时间:2016-12-14 03:26:15

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

我正在尝试测试我正在尝试创建的新标记帮助程序。我偶然发现了新的.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 /*).

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

2 个答案:

答案 0 :(得分:1)

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

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

HTMLTargetElement的第一个参数是您要定位的标记,因此在您的情况下将是&#34; span&#34;。你在那里输入了一个班级名称。

这样至少你的断点会被击中,从那里我确定你会弄明白其余的。

答案 1 :(得分: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/

相关问题