HttpServerUtility.UrlDecode 方法

定義

解碼一個字串,該字串被編碼用於 HTTP 傳輸,然後以 URL 形式傳送給伺服器。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

多載

名稱 Description
UrlDecode(String)

URL 解碼一個字串,並回傳解碼後的字串。

UrlDecode(String, TextWriter)

將接收到的 URL HTML 字串解碼,並將結果輸出傳送到輸出 TextWriter 串流。

UrlDecode(String)

URL 解碼一個字串,並回傳解碼後的字串。

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

要解碼的文字字串。

傳回

解碼後的文字。

範例

以下範例說明如何從查詢字串中擷取的值進行 URL 解碼。 程式碼存在於網頁的程式碼背後檔案中。 ReturnPage指的是控制。HyperLink

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 解碼一個值。

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 字串中的文字。 像是問號(?)、&符號(&)、斜線(/)和空格等字元,可能會被某些瀏覽器截斷或損壞。 因此,這些字元必須 <a> 以標籤或查詢字串編碼,瀏覽器可將字串以請求字串重新傳送。

此方法是從 ASP.NET 應用程式在執行時存取 HttpUtility.UrlDecode 方法的方便方式。 內部而言,此方法用於 HttpUtility.UrlDecode 解碼字串。

在 ASP.NET 網頁的程式碼背後檔案中,透過 HttpServerUtility 屬性存取 Server 類別的實例。 在不包含程式碼後方檔案的類別中,使用 來 HttpContext.Current.Server 存取該 HttpServerUtility 類別的實例。

在網頁應用程式之外,使用 WebUtility 該類別來編碼或解碼數值。

適用於

UrlDecode(String, TextWriter)

將接收到的 URL HTML 字串解碼,並將結果輸出傳送到輸出 TextWriter 串流。

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

要解碼的 HTML 字串。

output
TextWriter

包含解碼字串的 TextWriter 輸出串流。

範例

以下範例將名為( EncodedString 以 URL 接收)的字串解碼為名為 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 字串中的文字。 像是問號(?)、&符號(&)、斜線(/)和空格等字元,可能會被某些瀏覽器截斷或損壞。 因此,這些字元必須 <a> 以標籤或查詢字串編碼,瀏覽器可將字串以請求字串重新傳送。

UrlDecode 是從 ASP.NET 應用程式在執行時方便存取 HttpUtility.UrlDecode 方法的方法。 內部用 UrlDecodeHttpUtility.UrlDecode 來解碼字串。

若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。

適用於