BanksySan
BanksySan

Reputation: 28510

Get tag content in TagHelper

If I have a tag like:

<foo>Some content</foo>

How can I get the content in a TagHelper?

I can't see anything on TagHelper or TagHelperContext.

I'm trying to parse the content of a tag.

Upvotes: 4

Views: 1149

Answers (1)

BanksySan
BanksySan

Reputation: 28510

The solution is a bit unintuitive, you get the content from the TagHelperOutput via the TagHelperOutput.GetChildContentAsync() method.

If we have a tag like so:

<my-tag>Some content</my-tag>

Then

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    var childContext = output.GetChildContentAsync().Result;
    var content = childContext.GetContent();
    // content == "Some content"
}

Upvotes: 9

Related Questions