HttpServerUtility.HtmlEncode 方法

定義

將字串編碼以顯示於瀏覽器。Encodes a string to be displayed in a browser.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

多載

HtmlEncode(String)

將字串作 HTML 編碼,並傳回編碼的字串。HTML-encodes a string and returns the encoded string.

HtmlEncode(String, TextWriter)

將字串作 HTML 編碼,並送出結果輸出至 TextWriter 輸出資料流。HTML-encodes a string and sends the resulting output to a TextWriter output stream.

HtmlEncode(String)

將字串作 HTML 編碼,並傳回編碼的字串。HTML-encodes a string and returns the encoded string.

public:
 System::String ^ HtmlEncode(System::String ^ s);
public string HtmlEncode (string s);
member this.HtmlEncode : string -> string
Public Function HtmlEncode (s As String) As String

參數

s
String

要編碼的文字字串。The text string to encode.

傳回

HTML 編碼的文字。The HTML-encoded text.

範例

下列範例示範如何對可能編碼不安全程式碼的值進行 HTML 編碼。The following example shows how to HTML-encode a value that potentially codes unsafe code. 程式碼位於網頁的程式碼後置檔案中。The code resides in the code-behind file for a web page. 在此範例中,要編碼的值是硬式編碼,只是為了簡化範例,並顯示您可能會進行 HTML 編碼的數值型別。The value to encode is hard-coded in this example only to simplify the example and show the type of value you might HTML-encode. 一般來說,您會對從使用者或要求收到的值進行 HTML 編碼。Typically, you would HTML-encode a value that you received from the user or the request. Result 是指 Literal 控制項。Result refers to a Literal control.

public partial class _Default : Page
{        
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = Server.HtmlEncode("<script>unsafe</script>");      
    } 
}
Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Result.Text = Server.HtmlEncode("<script>unsafe</script>")
    End Sub
End Class

下一個範例與前一個範例類似,不同之處在于它會示範如何從不在程式碼後置檔案中的類別內,對值進行 HTML 編碼。The next example is similar to the previous example except it shows how to HTML-encode a value from within a class that is not in the code-behind file.

public class SampleClass
{
    public string GetEncodedText()
    {
        return HttpContext.Current.Server.HtmlEncode("<script>unsafe</script>");
    }
}
Public Class SampleClass
    Public Function GetEncodedText() As String
        Return HttpContext.Current.Server.HtmlEncode("<script>unsafe</script>")
    End Function
End Class

備註

HTML 編碼可確保文字會正確地顯示在瀏覽器中,而且瀏覽器不會將其轉譯為 HTML。HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. 例如,如果文字字串包含小於符號(<)或大於符號(>),則瀏覽器會將這些字元轉譯為 HTML 標籤的開頭或右括弧。For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. 當字元以 HTML 編碼時,會將它們轉換成 &lt;&gt;的字串,這會導致瀏覽器顯示小於正負號且大於正負號的錯誤。When the characters are HTML encoded, they are converted to the strings &lt; and &gt;, which causes the browser to display the less than sign and greater than sign correctly.

這個方法是在執行時間從 ASP.NET 應用程式存取 HttpUtility.HtmlEncode 方法的便利方式。This method is a convenient way to access the HttpUtility.HtmlEncode method at run time from an ASP.NET application. 就內部而言,這個方法會使用 HttpUtility.HtmlEncode 來編碼字串。Internally, this method uses HttpUtility.HtmlEncode to encode strings.

在 ASP.NET 網頁的程式碼後置檔案中,透過 Server 屬性存取 HttpServerUtility 類別的實例。In the code-behind file for an ASP.NET web page, access an instance of the HttpServerUtility class through the Server property. 在不在程式碼後置檔案中的類別中,使用 HttpContext.Current.Server 來存取 HttpServerUtility 類別的實例。In a class that is not in a code-behind file, use HttpContext.Current.Server to access an instance of the HttpServerUtility class.

在 web 應用程式之外,請使用 WebUtility 類別來編碼或解碼值。Outside of a web application, use the WebUtility class to encode or decode values.

HtmlEncode(String, TextWriter)

將字串作 HTML 編碼,並送出結果輸出至 TextWriter 輸出資料流。HTML-encodes a string and sends the resulting output to a TextWriter output stream.

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

參數

s
String

要編碼的字串。The string to encode.

output
TextWriter

包含已編碼字串的 TextWriter 輸出資料流。The TextWriter output stream that contains the encoded string.

範例

下列範例會將字串編碼以供 HTTP 傳輸。The following example encodes a string for transmission by HTTP. 它會編碼名為 TestString的字串,其中包含 "This is a <Test String >." 文字,並將它複製到名為 EncodedString 的字串中,如同「這是 <測試字串>。」。It encodes the string named TestString, which contains the text "This is a <Test String>.", and copies it into the string named EncodedString as "This is a <Test String>.".

String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
   
Dim TestString As String = "This is a <Test String>."
Dim writer As New StringWriter
Server.HtmlEncode(TestString, writer)
Dim EncodedString As String = writer.ToString()
   

備註

HTML 編碼可確保文字會正確地顯示在瀏覽器中,瀏覽器不會將其轉譯為 HTML。HTML encoding ensures that text will be correctly displayed in the browser, not interpreted by the browser as HTML. 例如,如果文字字串包含小於符號(<)或大於符號(>),則瀏覽器會將這些字元轉譯為 HTML 標籤的左或右括弧。For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as an opening or closing bracket of an HTML tag. 這兩個字元的 HTML 編碼方式分別為 &lt;&gt;,這會導致瀏覽器顯示小於正負號且大於正負號的符號。The HTML encoding of these two characters is &lt; and &gt;, respectively, which causes the browser to display the less than sign and greater than sign correctly.

HtmlEncode 是在執行時間從 ASP.NET 應用程式存取 HttpUtility.HtmlEncode 方法的便利方式。HtmlEncode is a convenient way to access the HttpUtility.HtmlEncode method at run time from an ASP.NET application. 就內部而言,HtmlEncode 會使用 HttpUtility.HtmlEncode 來編碼字串。Internally, HtmlEncode uses HttpUtility.HtmlEncode to encode strings.

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。To encode or decode values outside of a web application, use the WebUtility class.

適用於