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
