Last active
January 18, 2019 15:26
-
-
Save sebnilsson/c40d21b37acc54b5558a51dfb755eb47 to your computer and use it in GitHub Desktop.
Render everything inside tag as encoded HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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