Search code examples
c#asp.net-coreasp.net-core-tag-helpers

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.


Solution

  • 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"
    }