HttpServerUtility.UrlEncode HttpServerUtility.UrlEncode HttpServerUtility.UrlEncode HttpServerUtility.UrlEncode Method

定義

經由 URL 將字串編碼,以進行從 Web 伺服器至用戶端的可靠 HTTP 傳輸。Encodes a string for reliable HTTP transmission from the Web server to a client through the URL.

多載

UrlEncode(String) UrlEncode(String) UrlEncode(String) UrlEncode(String)

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

UrlEncode(String, TextWriter) UrlEncode(String, TextWriter) UrlEncode(String, TextWriter) UrlEncode(String, TextWriter)

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

備註

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

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

UrlEncode(String) UrlEncode(String) UrlEncode(String) UrlEncode(String)

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

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

參數

s
String String String String

要作 URL 編碼的文字。The text to URL-encode.

傳回

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

範例

下列範例示範如何將 URL 編碼的值來當做超連結的查詢字串值。The following example shows how to URL-encode a value that is used as a query string value of a hyperlink. 程式碼所在的網頁的程式碼後置檔案中。The code resides in the code-behind file for a web page. 要編碼的值是硬式編碼在此範例中只是要簡化範例,及顯示值的型別,您可能會將 URL 編碼。The value to encode is hard-coded in this example only to simplify the example and show the type of value you might URL-encode. 一般而言,您會將 URL 編碼值,您有來自使用者或要求。Typically, you would URL-encode a value that you received from the user or the request. NextPage 是指HyperLink控制項。NextPage refers to a HyperLink control.

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string destinationURL = "http://www.contoso.com/default.aspx?user=test";

        NextPage.NavigateUrl = "~/Finish?url=" + Server.UrlEncode(destinationURL);
    }             
}
Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim destinationURL = "http://www.contoso.com/default.aspx?user=test"

        NextPage.NavigateUrl = "~/Finish?url=" + Server.UrlEncode(destinationURL)
    End Sub
End Class

下一個範例是類似於先前的範例,但它會顯示如何將 URL 編碼類別內的值不在程式碼後置檔案。The next example is similar to the previous example except it shows how to URL-encode a value from within a class that is not in the code-behind file.

public class SampleClass
{
    public string GetUrl()
    {
        string destinationURL = "http://www.contoso.com/default.aspx?user=test";

        return "~/Finish?url=" + HttpContext.Current.Server.UrlEncode(destinationURL);
    }
}
Public Class SampleClass
    Public Function GetUrl() As String
        Dim destinationURL = "http://www.contoso.com/default.aspx?user=test"

        Return "~/Finish?url=" + HttpContext.Current.Server.UrlEncode(destinationURL)
    End Function
End Class

備註

URL 編碼,可確保所有的瀏覽器將會正確傳輸 URL 字串中的文字。URL encoding ensures that all browsers will correctly transmit text in URL strings. 字元,例如問題標記 (?)、 連字號 (&)、 斜線符號 (/),和空格可能會被截斷或某些瀏覽器損毀。Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. 如此一來,這些字元必須編碼<a>標記,或在查詢字串的字串可能由瀏覽器要求字串重新傳送。As a result, these characters must be encoded in <a> tags or in query strings where the strings can be re-sent by a browser in a request string.

這個方法是便利的方式來存取HttpUtility.UrlEncode從 ASP.NET 應用程式的執行階段的方法。This method is a convenient way to access the HttpUtility.UrlEncode method at run time from an ASP.NET application. 就內部而言,這個方法會使用HttpUtility.UrlEncode編碼字串。Internally, this method uses HttpUtility.UrlEncode 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.

UrlEncode(String, TextWriter) UrlEncode(String, TextWriter) UrlEncode(String, TextWriter) UrlEncode(String, TextWriter)

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

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

參數

s
String String String String

要編碼的文字字串。The text 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為"此 + 是 + a + %3ctest + 字串 %3e。 」。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+%3cTest+String%3e.".

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

備註

URL 編碼,可確保所有的瀏覽器將會正確傳輸 URL 字串中的文字。URL encoding ensures that all browsers will correctly transmit text in URL strings. 字元,例如問題標記 (?)、 連字號 (&)、 斜線符號 (/),和空格可能會被截斷或某些瀏覽器損毀。Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. 如此一來,這些字元必須編碼<a>標記,或在查詢字串的字串可能由瀏覽器要求字串重新傳送。As a result, these characters must be encoded in <a> tags or in query strings where the strings can be re-sent by a browser in a request string.

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

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

適用於