Search code examples
c#html-encode

How to safely HTML encode content which may already be HTML encoded?


I need to HTML encode some text which may or may not already be HTML encoded (perhaps only partially). Is the following safe? Are there any characters/encodings that could cause unexpected behaviour?

HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))

Thanks


Solution

  • Your logic "HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))" is safe. There is a standard for HTML characters encoding. Take a look.