ASP.Net Core:从一个标签助手输出 2 个标签

Joh*_*ika 4 asp.net-core-mvc tag-helpers asp.net-core asp.net-core-2.0 asp.net-core-tag-helpers

使用 ASP.Net Core 的 Tag Helpers,有没有办法在根级别将 1 个标签转换为 2 个标签?我知道您可以使用 完全删除标签TagHelperOutput.TagName == null,但我想知道如何做相反的事情来输出多个标签。

例如,从:

<canonical href="/testing" />
Run Code Online (Sandbox Code Playgroud)

到:

<link rel="canonical" href="http://www.examples.com/widgets" />
<link rel="next" href="http://www.examples.com/widgets?page=2" />
Run Code Online (Sandbox Code Playgroud)

这是一个示例标签助手,它输出一个标签,但不是两个:

[HtmlTargetElement("canonical")]
public class CanonicalLinkTagHelper : TagHelper
{
    public string Href { get; set; }
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "link";
        output.Attributes.SetAttribute("rel", "canonical");
        output.Attributes.SetAttribute(new TagHelperAttribute("href", new HtmlString(Href)));
    }
}
Run Code Online (Sandbox Code Playgroud)

Nev*_*ane 5

根据此文档,一旦您习惯TagHelperOutput.TagName == null删除标签,您必须能够使用添加自定义 HTMLoutput.PostContent.AppendHtml()

更新

PostContent只是在后面追加。要替换整个内容,您需要使用output.Content.SetHtmlContent(