日期:2014-05-18  浏览次数:20357 次

HttpUtility.HtmlDecode与Server.HtmlDecode区别
HttpUtility.HtmlDecode
HttpUtility.HtmlEncode

Server.HtmlDecode
Server.HtmlEncode

请教一下他们有什么区别,还有HtmlDecode与HtmlEncode分别用在什么场合??????????????

他们与下面一般手工写的代码有什么不一样吗??????????????????

public static string htmlencode(string str)  
  {
  if (str == null || str == "")
  return "";
  str = str.Replace(">", ">");
  str = str.Replace("<", "&lt;");
  str = str.Replace(" ", "&nbsp;");
  str = str.Replace(" ", " &nbsp;");
  str = str.Replace("\"", "&quot;");
  str = str.Replace("\'", "&#39;");
  str = str.Replace("\n", "<br/> ");
  return str;
  }

------解决方案--------------------
HttpUtility.HtmlDecode //直接引用System.WebHttpUtility.HtmlEncode 
与 
Server.HtmlDecode //可用在继承Page的类中Server.HtmlEncode 

程序无区别


public static string htmlencode(string str)

if (str == null || str == "") 
return ""; 
str = str.Replace(">", "&gt;"); 
str = str.Replace(" <", "&lt;"); 
str = str.Replace(" ", "&nbsp;"); 
str = str.Replace(" ", " &nbsp;"); 
str = str.Replace("\"", "&quot;"); 
str = str.Replace("\'", "&#39;"); 
str = str.Replace("\n", " <br/> "); //htmlencode最少没这个
return str; 
}
------解决方案--------------------
HttpUtility.HtmlDecode 
HttpUtility.HtmlEncode 
与 
Server.HtmlDecode 
Server.HtmlEncode 
一样
------解决方案--------------------
URL 编码确保所有浏览器均正确地传输 URL 字符串中的文本。在某些浏览器中,像 ?、&、/ 和空格这样的字符可能会被截断或损坏,因此这些字符必须在 <a> 标记或查询字符串中编码,在查询字符串中编码时,浏览器能以请求字符串的形式重新发送这些字符串。
------解决方案--------------------
两者是一样的功能

转义 html 中的保留字符