TagHelperOutput represents the element a Tag Helper will render. A Process or ProcessAsync method can change its tag name, tag mode, attributes, and inner HTML before the response is generated. section 32.5 section 32.1.1
Use the output object to shape or suppress generated markup:
- Set TagName to choose the rendered element; set it to null to remove the element. section 32.1.2
- Set TagMode to control whether the element uses start and end tags or a self-closing form. section 32.1.1
- Use Content.SetContent() for input that should be HTML-encoded automatically, or use SetHtmlContent() only with already encoded HTML. section 32.1.1
- Call SuppressOutput() to prevent the element's inner content from rendering, and call GetChildContentAsync() to retrieve processed child content. section 32.5
Conditionally removing an element
An IfTagHelper receives TagHelperOutput in Process. When its if attribute is false, it sets output.TagName to null and calls output.SuppressOutput(), removing both the element and its content. When the value is true, the existing markup renders normally. section 32.1.2
Listing 32.1 SystemInfoTagHelper to render system information to a view
public class SystemInfoTagHelper : TagHelper #1
{
private readonly HtmlEncoder _htmlEncoder; #2
public SystemInfoTagHelper(HtmlEncoder htmlEncoder) #2
{
_htmlEncoder = htmlEncoder;
}
[HtmlAttributeName("add-machine")] #3
public bool IncludeMachine { get; set; } = true;
[HtmlAttributeName("add-os")] #3
public bool IncludeOS { get; set; } = true;
public override void Process( #4
TagHelperContext context, TagHelperOutput output) #4
{
output.TagName = "div"; #5
output.TagMode = TagMode.StartTagAndEndTag; #6
var sb = new StringBuilder();
if (IncludeMachine) #7
{ #7
sb.Append(" <strong>Machine</strong> "); #7
sb.Append(_htmlEncoder.Encode(Environment.MachineName)); #7
} #7
if (IncludeOS) #8
{ #8
sb.Append(" <strong>OS</strong> "); #8
sb.Append( #8
_htmlEncoder.Encode(RuntimeInformation.OSDescription)); #8
} #8
output.Content.SetHtmlContent(sb.ToString()); #9
}
}
The SystemInfoTagHelper listing shows TagHelperOutput being configured in a working custom Tag Helper, including the rendered tag and tag mode. section 32.1.1

