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.

注解

UrlEncodeHttpUtility.UrlEncode 在运行时从 ASP.NET 应用程序访问方法的一种简便方法。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.

返回

String

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. 某些浏览器可能会截断或损坏问号,如问号 (? ) ,and 号 (&) 、斜杠标记 (/) 和空格。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.UrlEncode 在运行时从 ASP.NET 应用程序访问方法的一种简便方法。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 通过属性访问类的实例 ServerIn the code-behind file for an ASP.NET web page, access an instance of the HttpServerUtility class through the Server property. 在不在代码隐藏文件中的类中,使用 HttpContext.Current.Server 访问类的实例 HttpServerUtilityIn 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. 某些浏览器可能会截断或损坏问号,如问号 (? ) ,and 号 (&) 、斜杠标记 (/) 和空格。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.

UrlEncodeHttpUtility.UrlEncode 在运行时从 ASP.NET 应用程序访问方法的一种简便方法。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.

适用于