Asp.net core 在ASP.NET Core 1.1中错误呈现TagHelper

Asp.net core 在ASP.NET Core 1.1中错误呈现TagHelper,asp.net-core,asp.net-core-mvc,tag-helpers,asp.net-core-tag-helpers,Asp.net Core,Asp.net Core Mvc,Tag Helpers,Asp.net Core Tag Helpers,我制作了一个标记帮助器,如下所示: public class GooglePlusOneTagHelper : TagHelper { [ViewContext] public ViewContext ViewContext { get; set; } public string Size { get; set; } public string Annotation { get; set; } public override void Process(Ta

我制作了一个标记帮助器,如下所示:

public class GooglePlusOneTagHelper : TagHelper {
   [ViewContext]
    public ViewContext ViewContext { get; set; }
    public string Size { get; set; }
    public string Annotation { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output) {
        output.TagMode = TagMode.StartTagAndEndTag;
        output.TagName = "div";

        var request = ViewContext.HttpContext.Request;
        var absoluteUri = string.Concat(
            request.Scheme,
            "://",
            request.Host.ToUriComponent(),
            request.PathBase.ToUriComponent(),
            request.Path.ToUriComponent(),
            request.QueryString.ToUriComponent());

        output.Attributes.SetAttribute("class", "g-plusone");

        string size = Size.ToLower().Trim() ?? "";
        if (size == "small" || size == "medium" || size == "tall")
            output.Attributes.SetAttribute("data-size", size);

        string annotation = Annotation.ToLower().Trim() ?? "";
        if (annotation == "none" || annotation == "inline")
            output.Attributes.SetAttribute("data-annotation", annotation);

        output.Attributes.SetAttribute("data-href", absoluteUri);
    }
}
<google-plusone size="medium" annotation="bubble"  >
当我把这个代码放到一个视图中:

<google-plusone size="medium" annotation="bubble"  />

它在视图中的渲染方式如下:

public class GooglePlusOneTagHelper : TagHelper {
   [ViewContext]
    public ViewContext ViewContext { get; set; }
    public string Size { get; set; }
    public string Annotation { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output) {
        output.TagMode = TagMode.StartTagAndEndTag;
        output.TagName = "div";

        var request = ViewContext.HttpContext.Request;
        var absoluteUri = string.Concat(
            request.Scheme,
            "://",
            request.Host.ToUriComponent(),
            request.PathBase.ToUriComponent(),
            request.Path.ToUriComponent(),
            request.QueryString.ToUriComponent());

        output.Attributes.SetAttribute("class", "g-plusone");

        string size = Size.ToLower().Trim() ?? "";
        if (size == "small" || size == "medium" || size == "tall")
            output.Attributes.SetAttribute("data-size", size);

        string annotation = Annotation.ToLower().Trim() ?? "";
        if (annotation == "none" || annotation == "inline")
            output.Attributes.SetAttribute("data-annotation", annotation);

        output.Attributes.SetAttribute("data-href", absoluteUri);
    }
}
<google-plusone size="medium" annotation="bubble"  >


因此,标记助手不知何故没有处理它,因为它必须真正输出一个DIV。此外,它将我的原始标记从一个自动关闭转换为一个打开且从不关闭的标记

看起来标记辅助对象未正确渲染。以下是您可以验证的内容

  • 希望您在_ViewImports.cshtml文件中添加了
    @addTagHelper*,
    。如果没有,您需要添加它
  • 在项目文件中包括对Microsoft.AspNetCore.Mvc.TagHelpers的引用
  • [HtmlTargetElement(“google plusone”)]
    属性添加到标记帮助器
  • 这是我的HTML输出


    这是屏幕截图。

    标记帮助程序将标记帮助程序的Pascal cased C#类名称和属性转换为小写的烤肉串

    因此,对于Razor中的
    GooglePlusOneTagHelper
    ,您将编写
    ,而不是


    如果您想使用
    ,请按照@Anuraj的建议,将
    [HtmlTargetElement(“google plusone”)]
    属性添加到标记帮助器。

    我想我记得,您不能使用空/自动关闭标记(如果您想通过标记帮助器向标记添加内容)。尝试
    它不能用于呈现具有空内容但具有属性的DIV吗@TsengSee和相关的GitHub问题,其中内容无法使用自动关闭标记正确呈现(即,
    不会显示验证消息,但
    基本上,经验法则是,如果要呈现标记内的内容,它不能是自动关闭的
    vs
    。但我没有呈现内容,它应该呈现为奇怪的,我可以使用在同一外部DLL中定义的所有其他标记帮助程序,但这个没有t工作,我已经完成了以上所有工作。