如何安全地HTML编码可能已经是HTML编码的内容?

时间:2011-01-11 10:35:26

标签: c# html-encode

我需要对某些文本进行HTML编码,这些文本可能已经或可能不是HTML编码(可能只是部分编码)。以下是安全的吗?是否有任何可能导致意外行为的字符/编码?

HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))

由于

2 个答案:

答案 0 :(得分:1)

您的逻辑“HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(text))”是安全的。 HTML字符编码有一个标准。 Take a look.

答案 1 :(得分:0)

没有内置功能。但我会用:

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