HttpServerUtility.HtmlEncode HttpServerUtility.HtmlEncode HttpServerUtility.HtmlEncode HttpServerUtility.HtmlEncode Method

定義

將字串編碼以顯示於瀏覽器。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) HtmlEncode(String) HtmlEncode(String) HtmlEncode(String)

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

HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter)

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

HtmlEncode(String) HtmlEncode(String) HtmlEncode(String) 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 String String String

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

傳回

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

範例

下列範例會示範如何將 HTML 編碼的值,可能碼 unsafe 程式碼。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.

這個方法是便利的方式來存取HttpUtility.HtmlEncode從 ASP.NET 應用程式的執行階段的方法。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 web 網頁的程式碼後置檔案中,存取的執行個體HttpServerUtility類別透過Server屬性。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) HtmlEncode(String, TextWriter) HtmlEncode(String, TextWriter) 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 String String String

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

output
TextWriter TextWriter TextWriter TextWriter

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

範例

下列範例將字串編碼以進行 HTTP 傳輸。The following example encodes a string for transmission by HTTP. 它將字串編碼以名為TestString,其中包含文字"這是<測試字串 >。 」,並將它複製到名為字串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 是便利的方式來存取HttpUtility.HtmlEncode從 ASP.NET 應用程式的執行階段的方法。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.

適用於