自定义标签助手调试
我正在尝试测试我要创建的新标签助手.我偶然发现了新的.net核心验证的缺点,并且无法更改类后验证.因此,如果我想为错误提供红色背景,则跨度始终存在并且不会改变.因此,我决定制作自己的标签助手.问题是我似乎无法使其正常工作或触发.我什至无法达到断点.到目前为止,这是我需要的标签帮手.
I'm trying to test a new tag helper that I'm trying to create. I've stumbled into a shortcoming of the new .net core validation and cannot change the class post validation. So if I wanted to give my errors a red background, the span is always there and won't change. So, I've decided to make my own tag helper. the problem is I can't seem to get it to work or trigger. I can't even get it to hit a break point. Here is what I have for a tag helper so far.
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;
}
}
}
这是我的注册视图中的用法.
and here is the usage in my register view.
<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文件中注册了项目程序集名称.
And yes I have register in my _ViewImports.cshtml file, the projects assembly name.
@using MusicianProject
@using MusicianProject.Models
@addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
@addTagHelper "*, MusicianProject"
现在,我不确定某些文件的放置是否重要,但是_ViewImports.cshtml文件位于我的views文件夹根目录(src/Views/_ViewImports.cshtml)中,而我的标签助手在根目录中有自己的文件夹(src/TagHelpers/*).
Now I'm not sure if placement of certain files matters, but my _ViewImports.cshtml file is located within my views folder root (src/Views/_ViewImports.cshtml), and my tag helpers have their own folder in the root (src/TagHelpers/*).
我错过了什么,我该如何纠正?
What did I miss and how can I correct it?
您有2个问题
1- HtmlTargetElement
是可以在其中使用此标签助手的标签名称,例如:span,div,table ...
2-您没有在代码中使用标签助手,因此,它永远不会被解雇.默认情况下,它不会应用于HtmlTargetElement
中指定的所有标签,您应该通过在span标签中添加validate-class
属性来调用它.
1- The HtmlTargetElement
is the name of the tag where this tag helper can be used, ex: span, div, table ...
2- You didn't use the tag helper in your code, accordingly, it will never get fired. it is not applied by default to all the tags specified in the HtmlTargetElement
, you should call it by adding the validate-class
attribute to the span tag.
要了解有关标记帮助器的更多信息,请检查此链接 https://blogs.msdn.microsoft.com/msgulfcommunity/2015/06/17/developing-custom-tag-helpers-in-asp-net-5/
To know more about tag helpers, please check this link https://blogs.msdn.microsoft.com/msgulfcommunity/2015/06/17/developing-custom-tag-helpers-in-asp-net-5/