简体   繁体   中英

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

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

There is no built-in functionality. But I would use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM