HttpServerUtility.UrlDecode HttpServerUtility.UrlDecode HttpServerUtility.UrlDecode HttpServerUtility.UrlDecode Method

定義

將編碼為 HTTP 傳輸的字串解碼,並以 URL 送至伺服器。Decodes a string that was encoded for HTTP transmission and then sent to the server in a URL.

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

多載

UrlDecode(String) UrlDecode(String) UrlDecode(String) UrlDecode(String)

將字串作 URL 解碼,並傳回解碼的字串。URL-decodes a string and returns the decoded string.

UrlDecode(String, TextWriter) UrlDecode(String, TextWriter) UrlDecode(String, TextWriter) UrlDecode(String, TextWriter)

將 URL 中收到的 HTML 字串解碼,並送出結果輸出至 TextWriter 輸出資料流。Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.

UrlDecode(String) UrlDecode(String) UrlDecode(String) UrlDecode(String)

將字串作 URL 解碼,並傳回解碼的字串。URL-decodes a string and returns the decoded string.

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

參數

s
String String String String

要解碼的文字字串。The text string to decode.

傳回

解碼的文字。The decoded text.

範例

下列範例示範如何進行 URL 解碼值從擷取的查詢字串。The following example shows how to URL-decode a value that is retrieved from the query string. 程式碼所在的網頁的程式碼後置檔案中。The code resides in the code-behind file for a web page. ReturnPage 是指HyperLink控制項。ReturnPage refers to a HyperLink control.

public partial class _Default : Page
{       
    protected void Page_Load(object sender, EventArgs e)
    {
        string returnUrl = Server.UrlDecode(Request.QueryString["url"]);
        ReturnPage.NavigateUrl = returnUrl;
    }
}
Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim returnUrl = Server.UrlDecode(Request.QueryString("url"))
        ReturnPage.NavigateUrl = returnUrl
    End Sub
End Class

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

public class SampleClass
{
    public string RetrievePassedUrl()
    {
        return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString["url"]);
    }
}
Public Class SampleClass
    Public Function RetrievePassedUrl() As String
        Return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString("url"))
    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.UrlDecode從 ASP.NET 應用程式的執行階段的方法。This method is a convenient way to access the HttpUtility.UrlDecode method at run time from an ASP.NET application. 就內部而言,這個方法會使用HttpUtility.UrlDecode来解碼的字串。Internally, this method uses HttpUtility.UrlDecode to decode 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.

UrlDecode(String, TextWriter) UrlDecode(String, TextWriter) UrlDecode(String, TextWriter) UrlDecode(String, TextWriter)

將 URL 中收到的 HTML 字串解碼,並送出結果輸出至 TextWriter 輸出資料流。Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.

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

參數

s
String String String String

要解碼的 HTML 字串。The HTML string to decode.

output
TextWriter TextWriter TextWriter TextWriter

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

範例

下列範例會將名為字串的解碼EncodedString(在 URL 中收到) 至名為字串DecodedStringThe following example decodes the string named EncodedString (received in a URL) into the string named DecodedString.

StringWriter writer = new StringWriter();
Server.UrlDecode(EncodedString, writer);
String DecodedString = writer.ToString();
   
Dim writer As New StringWriter
Server.UrlDecode(EncodedString, writer)
Dim DecodedString 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.

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

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

適用於