HttpUtility.HtmlEncode 與 Server.HtmlEncode 的不同?

  • 3399
  • 0
  • C#
  • 2014-01-25

摘要:HttpUtility.HtmlEncode vs Server.HtmlEncode

在 .net 裡面 , 常常會使用到 HtmlEncode 方法 , 通常會有下列幾種

 

1. HttpUtility.HtmlEncode ( HttpServerUtility  內部實作 HttpUtility.HtmlEncode )

2. Server.HtmlEncode ( Server object 代表 HttpServerUtility 的實體 )

3. AntiXss Library

 

 除了AntiXss 是使用白名單的方式 , 其餘都是使用黑名單的方式來 Encode Html

 

使用黑名單 Encode 的實作大致一樣 ,  Encode 常見的 < 等 html 符號 , 

 

character 在 160 ~ 255 區間的 , 會被轉換成數字型態  , 例如 &#255 , 在 html 的顯示上會是個空白 , 

 

既然 HttpUtility.HtmlEncode 和 Server.HtmlEncode 的實做一樣為何還要多創個方法 ?

 

Server.HtmlEncode 會存在的原因 , 可以參考下列文章 , 其理由大致是如下 :

 

The reason for existence of Server.HtmlEncode is compatibility with classic ASP
 

 

 

參考資料 :

Server.HTMLEncode Method

Char 對照表