【C#】C#中的HtmlEncode与HtmlDecode:HttpUtility.HtmlEncode,HttpUtility.HtmlDecode,Server.HtmlEncode,Server

  HtmlEncode(String) 将字符串转换为 HTML 编码字符串。html

  HtmlDecode(String) 将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。web

  在web端项目中一般使用HttpUtility.HtmlEecodeHttpUtility.HtmlDecodeServer.HtmlEncodeServer.HtmlDecodeapi

  在C端项目中一般使用WebUtility.HtmlEncodeWebUtility.HtmlDecodeasp.net

  在说HttpUtility.HtmlEecode,HttpUtility.HtmlDecode,Server.HtmlEncode,Server.HtmlDecode这几个以前呢,得先说下HttpServerUtility类。函数

 #HttpServerUtility 类

  提供用于处理 Web 请求的 Helper 方法。工具

  html编码相关的方法以下:编码

  这个类没有构造函数,因此不能直接new出来一个实例;spa

  但三种种状况会返回这个类的实例。.net

一、在asp.net 中的aspx.cs文件中可获取到(Page.Server):

二、在asp.net中的通常处理程序ashx文件中可获取到(HttpContext.Server):

三、在MVC的项目中的controller文件中可获取到(Controller.Server):

 

#HttpUtility 类

  提供在处理 Web 请求时用于编码和解码 URL 的方法。 此类不能被继承。3d

  HttpUtility类在内部使用HttpServerUtility类,其方法和属性经过内部 ASP.NETServer对象对外公开。 此外,HttpUtility类包含编码和解码实用工具方法,当不能访问Server时,能够使用它。

该类在web项目中均可以使用:

 #WebUtility 类

  在C端项目中使用System.Net.WebUtility类进行Html编解码:WebUtility.HtmlEncodeWebUtility.HtmlDecode

 

 

#参考:

http://www.javashuo.com/article/p-huzaroxe-mm.html

http://www.javashuo.com/article/p-udalgvqr-kx.html

http://www.javashuo.com/article/p-puxooysu-h.html

相关文章
相关标签/搜索