zoukankan      html  css  js  c++  java
  • 【C#】C#中的HtmlEncode与HtmlDecode:HttpUtility.HtmlEncode,HttpUtility.HtmlDecode,Server.HtmlEncode,Server.HtmlDecode,WebUtility.HtmlEncode,WebUtility.HtmlDecode

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

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

      在web端项目中通常使用HttpUtility.HtmlEecodeHttpUtility.HtmlDecodeServer.HtmlEncodeServer.HtmlDecode

      在C端项目中通常使用WebUtility.HtmlEncodeWebUtility.HtmlDecode

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

     #HttpServerUtility 类

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

      html编码相关的方法如下:

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

      但三种种情况会返回这个类的实例。

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

    2、在asp.net中的一般处理程序ashx文件中可获取到(HttpContext.Server):

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

     

    #HttpUtility 类

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

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

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

     #WebUtility 类

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

     

    #参考:

    https://www.cnblogs.com/Sea1ee/p/7298959.html

    https://www.cnblogs.com/pangjie/p/3236918.html

    https://www.cnblogs.com/xdp-gacl/p/3722642.html

  • 相关阅读:
    923c C. Perfect Security
    hdu ACM Steps Section 1 花式A+B 输入输出格式
    我回来了
    POJ–2104 K-th Number
    bzoj1009: [HNOI2008]GT考试
    bzoj1875: [SDOI2009]HH去散步
    1898: [Zjoi2005]Swamp 沼泽鳄鱼
    Hadoop
    程序员的自我修养
    Effective C++笔记
  • 原文地址:https://www.cnblogs.com/willingtolove/p/10971194.html
Copyright © 2011-2022 走看看