Roman Pokrovskij
Roman Pokrovskij

Reputation: 9776

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

How to get elements defined as TagHelper content?

E.g. element defined as:

<markdown>bla bla</markdown>

And helper defined as:

[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"?
    }
}

Upvotes: 12

Views: 3184

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

You can use output.GetChildContentAsync() as explained in the docs (worth reading as it contains a few examples that retrieve the element's contents).

You will then implement your tag helper as in:

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

Upvotes: 17

Related Questions