【发布时间】:2015-08-01 15:05:18
【问题描述】:
我正在尝试在 MVC 6 中创建自定义标签助手,但无法使其工作。
这是我在 web 应用项目中定义的演示标签助手类。
namespace Microsoft.AspNet.Mvc.TagHelpers
{
[TargetElement("demo", Attributes = CustomAttributeName)]
public class DemoTagHelper : TagHelper
{
private const string CustomAttributeName = "asp-custom";
[HtmlAttributeName(CustomAttributeName)]
public string Custom { get; set; }
public string Value { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.Attributes["foo"] = "bar";
}
}
}
这就是我在视图中使用它的方式:
<demo asp-custom="hello world!">
Please work this time :)
</demo>
我尝试了很多东西。删除了 TargetElement 属性或更改了命名空间。没有什么变化...
结果还是一样。
顺便说一下,我的 Microsoft.AspNet.Mvc.TagHelpers 版本是 6.0.0-beta4。
也许我必须在某个地方注册我的标签助手?我查看了 MVC 源代码,他们没有在任何地方引用自己的标签助手。所以我认为不需要注册。
问题出在哪里?
【问题讨论】:
-
请参阅docs.asp.net/projects/mvc/en/latest/views/tag-helpers/…,了解如何创建自定义标签助手。
标签: c# asp.net asp.net-mvc asp.net-core-mvc tag-helpers