liveBook · Manning

SetHtmlContent

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

SetHtmlContent is a TagHelperOutput method that assigns HTML content without automatically encoding it.

SetHtmlContent is a TagHelperOutput method for assigning the HTML rendered inside an element. Because it does not automatically encode its input, encode output before passing it to this method. section 32.1.1

Rendering system information

A SystemInfoTagHelper changes the output element to a div and uses output.Content.SetHtmlContent() to place machine and operating-system information inside it. For unencoded input, use SetContent instead so the output is automatically HTML-encoded. section 32.1.1

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
    }
}

Inspect the complete SystemInfoTagHelper example, including its Process method and output configuration. section 32.1.1

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

How deep it goes
Explains content assignment and encoding safety, but does not detail the method implementation.
Examples
Uses SystemInfoTagHelper to render machine and operating-system information inside a div.
What it assumes
Assumes familiarity with Tag Helpers and Process, while warning about HTML encoding at the point of use.
What you can do after
Apply it via TagHelperOutput to place generated HTML inside a rendered element.
How it connects
Connects Tag Helpers, TagHelperOutput, SetContent, and HTML encoding.

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.