HttpUtility.HtmlEncode 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將字串轉換成 HTML 編碼的字串。
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。
多載
| 名稱 | Description |
|---|---|
| HtmlEncode(Object) |
將物件的字串表示轉換為 HTML 編碼的字串,並回傳編碼後的字串。 |
| HtmlEncode(String) |
將字串轉換成 HTML 編碼字串。 |
| HtmlEncode(String, TextWriter) |
將字串轉換成 HTML 編碼的字串,並以輸出串流回傳 TextWriter 。 |
HtmlEncode(Object)
將物件的字串表示轉換為 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
物件。
傳回
一個編碼字串。
備註
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。
適用於
HtmlEncode(String)
將字串轉換成 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該HtmlDecode類別的 和 HttpUtility 方法。 輸入字串是使用 該 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 解碼則是反向編碼。 例如,當嵌入文字區塊時,字元 < 和 > 會被編碼為 < 和 > 以進行 HTTP 傳輸。
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。
另請參閱
適用於
HtmlEncode(String, TextWriter)
將字串轉換成 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 解碼則是反向編碼。 例如,當嵌入文字區塊時,字元 < 和 >會被編碼為 < 和 > 以 HTTP 傳輸。
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。