HttpServerUtility.UrlEncode 方法

定義

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

多載

UrlEncode(String)

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

UrlEncode(String, TextWriter)

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

備註

UrlEncode是在執行時間從 ASP.NET 應用HttpUtility.UrlEncode程式存取方法的便利方式。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)

將字串作 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

要作 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.

這個方法是在執行時間從 ASP.NET 應用HttpUtility.UrlEncode程式存取方法的便利方式。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 網頁的程式碼後置檔案中, 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)

將字串作 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

要編碼的文字字串。The text 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為 "this + is + a +% 3cTest + string% 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是在執行時間從 ASP.NET 應用HttpUtility.UrlEncode程式存取方法的便利方式。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.

適用於