HttpUtility.HtmlEncode 方法

定義

將字串轉換成 HTML 編碼字串。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

多載

HtmlEncode(Object)

將物件的字串表示轉換成 HTML 編碼字串,並傳回此編碼的字串。

HtmlEncode(String)

將字串轉換成 HTML 編碼字串。

HtmlEncode(String, TextWriter)

將字串轉換成 HTML 編碼字串,並以 TextWriter 輸出資料流的形式傳回輸出。

HtmlEncode(Object)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs

將物件的字串表示轉換成 HTML 編碼字串,並傳回此編碼的字串。

public:
 static System::String ^ HtmlEncode(System::Object ^ value);
public static string? HtmlEncode (object? value);
public static string HtmlEncode (object value);
static member HtmlEncode : obj -> string
Public Shared Function HtmlEncode (value As Object) As String

參數

value
Object

物件。

傳回

編碼字串。

備註

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

適用於

HtmlEncode(String)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs

將字串轉換成 HTML 編碼字串。

public:
 static System::String ^ HtmlEncode(System::String ^ s);
public static string? HtmlEncode (string? s);
public static string HtmlEncode (string s);
static member HtmlEncode : string -> string
Public Shared Function HtmlEncode (s As String) As String

參數

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}");
    }
}
Imports System.Web
Imports System.IO

Class MyNewClass
   Public Shared Sub Main()
      Dim myString As String
      Console.WriteLine("Enter a string having '&' or '""'  in it: ")
      myString = Console.ReadLine()
      Dim myEncodedString As String
      ' Encode the string.
      myEncodedString = HttpUtility.HtmlEncode(myString)
      Console.WriteLine("HTML Encoded string is " + myEncodedString)
      Dim myWriter As New StringWriter()
      ' Decode the encoded string.
      HttpUtility.HtmlDecode(myEncodedString, myWriter)
      Console.Write("Decoded string of the above encoded string is " + myWriter.ToString())
   End Sub
End Class

備註

如果在 HTTP 資料流程中傳遞空白和標點符號之類的字元,在接收端可能會誤譯這些字元。 HTML 編碼會將 HTML 中不允許的字元轉換成字元實體對等專案;HTML 解碼會反轉編碼。 例如,當內嵌在文字區塊中時,字元 < 和 > 會編碼為 &lt;&gt; 進行 HTTP 傳輸。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於

HtmlEncode(String, TextWriter)

來源:
HttpUtility.cs
來源:
HttpUtility.cs
來源:
HttpUtility.cs

將字串轉換成 HTML 編碼字串,並以 TextWriter 輸出資料流的形式傳回輸出。

public:
 static void HtmlEncode(System::String ^ s, System::IO::TextWriter ^ output);
public static void HtmlEncode (string? s, System.IO.TextWriter output);
public static void HtmlEncode (string s, System.IO.TextWriter output);
static member HtmlEncode : string * System.IO.TextWriter -> unit
Public Shared Sub HtmlEncode (s As String, output As TextWriter)

參數

s
String

要編碼的字串。

output
TextWriter

TextWriter 輸出資料流。

備註

如果在 HTTP 資料流程中傳遞空白和標點符號之類的字元,在接收端可能會誤譯這些字元。 HTML 編碼會將 HTML 中不允許的字元轉換成字元實體對等專案;HTML 解碼會反轉編碼。 例如,當內嵌在文字區塊中時,字元 < 和 會 > 編碼為 &lt;&gt; 進行 HTTP 傳輸。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

另請參閱

適用於