HttpServerUtility.UrlEncode 方法

定义

通过 URL 对字符串进行编码,以便从 Web 服务器到客户端的可靠 HTTP 传输。

重载

名称 说明
UrlEncode(String)

URL 对字符串进行编码并返回编码的字符串。

UrlEncode(String, TextWriter)

URL 对字符串进行编码,并将生成的输出发送到 TextWriter 输出流。

注解

UrlEncode是一种在运行时从 ASP.NET 应用程序访问 HttpUtility.UrlEncode 方法的便捷方法。 在内部, UrlEncode 用于 HttpUtility.UrlEncode 对字符串进行编码。

若要对 Web 应用程序外部的值进行编码或解码,请使用类 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 编码。 代码驻留在网页的代码隐藏文件中。 在此示例中,要编码的值是硬编码的,只是为了简化示例并显示可以进行 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 网页的代码隐藏文件中,通过 Server 属性访问 HttpServerUtility 类的实例。 在不在代码隐藏文件中的类中,用于 HttpContext.Current.Server 访问类的 HttpServerUtility 实例。

在 Web 应用程序之外,使用 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+%3cTest+String%3e.”的 EncodedString 字符串中。

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 方法的便捷方法。 在内部, UrlEncode 用于 HttpUtility.UrlEncode 对字符串进行编码。

若要对 Web 应用程序外部的值进行编码或解码,请使用类 WebUtility

适用于