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

1

12

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"?
    }
}
Grannia answered 28/11, 2016 at 17:28 Comment(4)
Is that at runtime or design time? If it's the second you could find all classes implementing ITagHelperin the assemblies added in your _ViewImports.cshtml. In the first case I don't know if ASP.Net Core exposes such informationPurpose
At runtime. Content of element. Inside TagHelper.Proccess.Grannia
Ah, I totally misread the question. Yes, that is possible usingoutput.GetChildContentAsync()Purpose
see also this answer: https://mcmap.net/q/1009056/-taghelper-cached-output-by-calling-getchildcontentasync-and-content-getcontentBirgit
P
17

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
}
Purpose answered 29/11, 2016 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.