How to safely encode HTML code that can already be encoded in HTML format?

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

HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text)) 

thanks

+4
source share
2 answers

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

+1
source

No built-in features. But I would use:

  return HttpUtility.HtmlDecode(text)!=text ? text : HttpUtility.HtmlEncode(text); 
0
source

Source: https://habr.com/ru/post/1335063/


All Articles