HttpServerUtility.UrlEncode 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
編碼字串,確保網頁伺服器透過 URL 可靠地將 HTTP 傳輸給用戶端。
多載
| 名稱 | Description |
|---|---|
| UrlEncode(String) |
URL 編碼字串並回傳編碼後的字串。 |
| UrlEncode(String, TextWriter) |
URL 編碼字串並將結果傳送到 TextWriter 輸出串流。 |
備註
UrlEncode 是從 ASP.NET 應用程式在執行時方便存取 HttpUtility.UrlEncode 方法的方法。 內部用 UrlEncodeHttpUtility.UrlEncode 來編碼字串。
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。
UrlEncode(String)
URL 編碼字串並回傳編碼後的字串。
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 編碼。
傳回
網址編碼的文字。
範例
以下範例展示了如何將用作超連結查詢字串值的值進行 URL 編碼。 程式碼存在於網頁的程式碼背後檔案中。 這個範例中要編碼的值是硬編碼的,目的是簡化範例並展示你可能用 URL 編碼的值類型。 通常,你會用網址編碼使用者或請求所接收的值。
NextPage指的是控制。HyperLink
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 編碼的值。
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 字串中的文字。 像是問號(?)、&符號(&)、斜線(/)和空格等字元,可能會被某些瀏覽器截斷或損壞。 因此,這些字元必須 <a> 以標籤或查詢字串編碼,瀏覽器可將字串以請求字串重新傳送。
此方法是從 ASP.NET 應用程式在執行時存取 HttpUtility.UrlEncode 方法的方便方式。 在內部,此方法用於 HttpUtility.UrlEncode 編碼字串。
在 ASP.NET 網頁的程式碼背後檔案中,透過 HttpServerUtility 屬性存取 Server 類別的實例。 在不包含程式碼後方檔案的類別中,使用 來 HttpContext.Current.Server 存取該 HttpServerUtility 類別的實例。
在網頁應用程式之外,使用 WebUtility 該類別來編碼或解碼數值。
適用於
UrlEncode(String, TextWriter)
URL 編碼字串並將結果傳送到 TextWriter 輸出串流。
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
要進行編碼的文字字串。
- output
- TextWriter
包含編碼字串的 TextWriter 輸出串流。
範例
以下範例編碼一個字串,供 HTTP 傳輸。 它編碼名為 TestString的字串,其中包含「This is <a Test String>.」的文字,並將其複製到名為 EncodedString 「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 字串中的文字。 像是問號(?)、&符號(&)、斜線(/)和空格等字元,可能會被某些瀏覽器截斷或損壞。 因此,這些字元必須 <a> 以標籤或查詢字串編碼,瀏覽器可將字串以請求字串重新傳送。
UrlEncode 是從 ASP.NET 應用程式在執行時方便存取 HttpUtility.UrlEncode 方法的方法。 內部用 UrlEncodeHttpUtility.UrlEncode 來編碼字串。
若要在網頁應用程式外編碼或解碼數值,請使用 該 WebUtility 類別。