【发布时间】:2019-04-02 22:39:45
【问题描述】:
使用 ASP.Net Core 的 Tag Helpers,有没有办法在根级别将 1 个标签转换为 2 个标签?我知道你可以使用TagHelperOutput.TagName == null 完全删除一个标签,但我想知道如何做相反的事情来输出多个标签。
例如,从:
<canonical href="/testing" />
到:
<link rel="canonical" href="http://www.examples.com/widgets" />
<link rel="next" href="http://www.examples.com/widgets?page=2" />
这是一个示例标签助手,它输出其中一个标签,但不能同时输出两个标签:
[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)));
}
}
【问题讨论】:
-
您是否尝试过删除标签并使用
output.PostContent.AppendHtml?
标签: asp.net-core asp.net-core-mvc asp.net-core-2.0 tag-helpers asp.net-core-tag-helpers