Can I use a Tag Helper in a custom Tag Helper that returns html?

后端 未结 3 688
梦毁少年i
梦毁少年i 2020-12-30 23:39

I recently ran into a situation where I would like to use a tag helper within a tag helper. I looked around and couldn\'t find anyone else trying to do this, am I using a po

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 23:54

    No you cannot. TagHelpers are a Razor parse time feature.

    One alternative is creating a TagHelper and manually invoking its ProcessAsync/Process method. Aka:

    var anchorTagHelper = new AnchorTagHelper
    {
        Action = "Home",
    };
    var anchorOutput = new TagHelperOutput("a", new TagHelperAttributeList(), (useCachedResult, encoder) => new HtmlString());
    var anchorContext = new TagHelperContext(
        new TagHelperAttributeList(new[] { new TagHelperAttribute("asp-action", new HtmlString("Home")) }),
        new Dictionary(),
        Guid.NewGuid());
    await anchorTagHelper.ProcessAsync(anchorContext, anchorOutput);
    output.Content.SetHtmlContent(anchorOutput);
    

提交回复
热议问题