Using a Tag Helper in an ASP.NET 5 (vNext) project

168 views Asked by At

I am learning ASP.NET 5 (vNext). I have created a basic TagHelper. My tag helper looks like this:

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);
      }
   }
}

Then, in my view, I have the following:

<my-tag my-count="3"></my-tag>

When I load the view, it loads fine (no server-side errors). However, my tag helper does not actually render the expected HTML. Instead, my-tag is written when I look at the source. What am I doing wrong?

1

There are 1 answers

0
Canavar On

You should add reference of the tag helpers assembly to your view, just like that :

@addTagHelper "*, MyCompany.UI"