我正在学习 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。我究竟做错了什么?