我正在学习ASP.NET 5(vNext)。我创建了一个基本的TagHelper。我的标记助手看起来像这样:
MyTagHelper.cs
namespace MyCompany.UI
{
[TargetElement("my-tag", Attributes="my-count")]
public class MyTagHelper : TagHelper
{
[HtmlAttributeName("my-count")]
public string MyCount { get; set; }
protected IHtmlGenerator Generator { get; }
public MyTagHelper(IHtmlGenerator generator)
{
this.Generator = generator;
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
uint theCount = 0;
UInt32.TryParse(MyCount, out theCount);
var list= new TagBuilder("ul");
for (var i=0; i<theCount; i++)
{
var item = new TagBuilder("li");
item.SetInnerText(i.ToString());
list.InnerHtml += item;
}
var html = list.ToHtmlString(TagRenderMode.Normal).ToString();
output.Content.Append(html);
}
}
}
然后,在我看来,我有以下内容:
<my-tag my-count="3"></my-tag>
当我加载视图时,它加载正常(没有服务器端错误)。但是,我的标记帮助程序实际上并不呈现预期的HTML。相反,当我查看源代码时会写入my-tag。我究竟做错了什么?
答案 0 :(得分:1)
您应该将标记帮助程序集的引用添加到视图中,就像这样:
@addTagHelper "*, MyCompany.UI"