liveBook · Manning

TagHelperOutput

As explained in ASP.NET Core in Action, 3e

TagHelperOutput is the ASP.NET Core object representing the generated element, attributes, and content produced by a Tag Helper.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains rendering properties, content methods, suppression, child-content retrieval, and execution ordering without descending into framework internals.
Examples
Uses SystemInfoTagHelper for generated content and IfTagHelper for removing an element when an if attribute is false.
What you can do after
Build custom Tag Helpers that generate, transform, encode, or suppress Razor HTML output.
How it connects
Connects Tag Helpers with Razor templates, HTML encoding, conditional rendering, and view components.

Also explained in

Compare how 2 books explain TagHelperOutput →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.