HttpUtility.HtmlEncode 方法

定义

将字符串转换为 HTML 编码格式的字符串。

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。

重载

HtmlEncode(Object)

将对象的字符串表示形式转换为 HTML 编码的字符串,并返回已编码的字符串。

HtmlEncode(String)

将字符串转换为 HTML 编码字符串。

HtmlEncode(String, TextWriter)

将字符串转换为 HTML 编码的字符串,并以 TextWriter 输出流的形式返回输出。

HtmlEncode(Object)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

将对象的字符串表示形式转换为 HTML 编码的字符串,并返回已编码的字符串。

public static string? HtmlEncode (object? value);
public static string HtmlEncode (object value);

参数

value
Object

一个对象。

返回

已编码的字符串。

注解

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HtmlEncode(String)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

将字符串转换为 HTML 编码字符串。

public static string? HtmlEncode (string? s);
public static string HtmlEncode (string s);

参数

s
String

要编码的字符串。

返回

已编码的字符串。

示例

下面的代码示例演示 HtmlEncode 类的 HttpUtilityHtmlDecode 方法。 使用 方法对输入字符串进行编码 HtmlEncode 。 然后,使用 HtmlDecode 方法解码获取的编码字符串。

using System;
using System.Web;
using System.IO;

class MyNewClass
{
    public static void Main()
    {
        Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
        string myString = Console.ReadLine();

        // Encode the string.
        string myEncodedString = HttpUtility.HtmlEncode(myString);

        Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
        StringWriter myWriter = new StringWriter();

        // Decode the encoded string.
        HttpUtility.HtmlDecode(myEncodedString, myWriter);

        string myDecodedString = myWriter.ToString();
        Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
    }
}

注解

如果在 HTTP 流中传递空格和标点符号等字符,则可能会在接收端被错误解释。 HTML 编码将 HTML 中不允许的字符转换为字符实体等效项;HTML 解码会反转编码。 例如,当嵌入到文本块中时,字符 < 和 > 将编码为 &lt;&gt; 以用于 HTTP 传输。

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

HtmlEncode(String, TextWriter)

Source:
HttpUtility.cs
Source:
HttpUtility.cs
Source:
HttpUtility.cs

将字符串转换为 HTML 编码的字符串,并以 TextWriter 输出流的形式返回输出。

public static void HtmlEncode (string? s, System.IO.TextWriter output);
public static void HtmlEncode (string s, System.IO.TextWriter output);

参数

s
String

要编码的字符串。

output
TextWriter

TextWriter 输出流。

注解

如果在 HTTP 流中传递空格和标点符号等字符,则可能会在接收端被错误解释。 HTML 编码将 HTML 中不允许的字符转换为字符实体等效项;HTML 解码会反转编码。 例如,当嵌入文本块中时,字符 < 和 >将编码为 &lt;&gt; ,以便进行 HTTP 传输。

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1