自定义TagHelpers无法渲染

时间:2016-12-07 09:20:11

标签: c# asp.net-core asp.net-core-tag-helpers

我在以下代码中定义了自定义TagHelper:

namespace TJ.Website.Core.TagHelpers { 

[HtmlTargetElement("pager", Attributes = "total-pages, current-page, link-url")]
public class PagerTagHelper : TagHelper {

    public int CurrentPage { get; set; }
    public int TotalPages { get; set; }

    [HtmlAttributeName("link-url")]
    public string Url { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output) {
        output.TagName = "div";
        output.PreContent.SetContent("<ul class=\"link-list\">");

        var items = new StringBuilder();
        for (var i = 1; i <= TotalPages; i++) {
            var li = new TagBuilder("li");

            var a = new TagBuilder("a");
            a.MergeAttribute("href", $"{Url}?page={i}");
            a.MergeAttribute("title", $"Click to go to page {i}");

            a.InnerHtml.Append(i.ToString());

            if (i == CurrentPage) {
                a.AddCssClass("active");
            }

            li.InnerHtml.Append(a.InnerHtml.ToString());

            items.AppendLine(li.ToString());
        }
        output.Content.SetContent(items.ToString());
        output.PostContent.SetContent("</ul>");
        output.Attributes.Clear();
        output.Attributes.Add("class", "pager");
    }
}
}

这是我的_ViewImports.cs.html页面

@using TJ.Website.Core
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, TJ.Website.Core
@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration     TelemetryConfiguration

在我的观看Views\Home\Index.cshtml我想要这样做

<pager total-pages='6' current-page='1'></pager>

但在我看来,它没有呈现...... /被识别出来 我的汇编/项目名称是TJ.Website.Core

请帮忙

0 个答案:

没有答案