Is it possible to call ViewComponent from custom TagHelper?

Viewed 1515

I'm writing a custom TagHelper and want to render a ViewComponent inside it. Something similar to what vc:xyz tag helper does, but in a more controlled way, so that I can determine at runtime which ViewComponent to render.

Is it possible?

2 Answers

A follow up on the answer.

There is no need to write end tag when calling the tag helper. Just set the TagMode in ProcessAsync:

output.TagMode = TagMode.StartTagAndEndTag;
output.Content.SetHtmlContent(content);

Then <widget name="abc" /> works perfectly fine.

Alternatively, you can render the content of the view component in place of the widget tag without rendering the tag itself:

output.SuppressOutput();
output.PostElement.SetHtmlContent(content);

I also noticed that adding self-closing tags in the view component's view makes them appear wrong in the final result, but that may be a different topic.

Related