如何获取 TagHelper.Process 中元素(定义为 TagHelper)的内容?

How to get element's (defined as TagHelper) content in TagHelper.Process?

如何获取定义为 TagHelper 内容的元素?

例如元素定义为:

<markdown>bla bla</markdown>

Helper 定义为:

[HtmlTargetElement("markdown")]
public class MarkdownTagHelper : TagHelper
{
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var c = output.Content.GetContent(); 
        // c is empty; how to get content "bla bla"?
    }
}

您可以按照 the docs 中的说明使用 output.GetChildContentAsync()(值得一读,因为它包含一些检索元素内容的示例)。

然后您将实现标签助手,如下所示:

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
      var c = (await output.GetChildContentAsync()).GetContent(); 
      // transform markdown in c
}