Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active January 18, 2019 15:26
Show Gist options
  • Save sebnilsson/c40d21b37acc54b5558a51dfb755eb47 to your computer and use it in GitHub Desktop.
Save sebnilsson/c40d21b37acc54b5558a51dfb755eb47 to your computer and use it in GitHub Desktop.
Render everything inside tag as encoded HTML
[HtmlTargetElement("html-encode", TagStructure = TagStructure.NormalOrSelfClosing)]
public class HtmlEncodeTagHelper : TagHelper
{
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var childContent = output.Content.IsModified
? output.Content.GetContent()
: (await output.GetChildContentAsync()).GetContent();
string encodedChildContent = WebUtility.HtmlEncode(childContent ?? string.Empty);
output.TagName = null;
output.Content.SetHtmlContent(encodedChildContent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment