Share via


HttpServerUtility.UrlDecode 方法

定義

將編碼為 HTTP 傳輸的字串解碼,並以 URL 送至伺服器。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

多載

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

下一個範例與上一個範例類似,不同之處在於它示範如何從不在程式代碼後置檔案中的類別中譯碼值。

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 字串中的文字。 問號 (?) 、ampersand (&) 、斜線標記 (/) 等字元,而某些瀏覽器可能會截斷或損毀空格。 因此,這些字元必須在標籤, <a> 或在查詢字串中,瀏覽器可以在要求字串中重新傳送字串。

此方法是一種方便的方式,可從 HttpUtility.UrlDecode ASP.NET 應用程式存取運行時間的方法。 在內部,這個方法會使用 HttpUtility.UrlDecode 來譯碼字串。

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

在 Web 應用程式之外,使用 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 輸出資料流。

範例

下列範例會將 URL) 中所收到的字串譯碼為名為 EncodedStringDecodedString的字串 (。

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 字串中的文字。 問號 (?) 、ampersand (&) 、斜線標記 (/) 等字元,而某些瀏覽器可能會截斷或損毀空格。 因此,這些字元必須在標籤, <a> 或在查詢字串中,瀏覽器可以在要求字串中重新傳送字串。

UrlDecode 是一種方便的方式,可從 ASP.NET 應用程式在運行時間存取 HttpUtility.UrlDecode 方法。 在內部使用 UrlDecodeHttpUtility.UrlDecode 來譯碼字串。

若要編碼或解碼 Web 應用程式之外的值,請使用 WebUtility 類別。

適用於