更新:2007 年 11 月
將 URL 字串的路徑部分以 URL 編碼,並傳回編碼的字串。
命名空間:
System.Web 組件:
System.Web (在 System.Web.dll 中)
Public Function UrlPathEncode ( _
s As String _
) As String
Dim instance As HttpServerUtility
Dim s As String
Dim returnValue As String
returnValue = instance.UrlPathEncode(s)
public string UrlPathEncode(
string s
)
public:
String^ UrlPathEncode(
String^ s
)
public String UrlPathEncode(
String s
)
public function UrlPathEncode(
s : String
) : String
URL 編碼方式確保所有瀏覽器將會正確傳輸 URL 字串中的文字。此類的字元,例如問號 (?)、連字號 (&)、斜線符號 (/) 和空格在某些瀏覽器中可能會遭到截斷或毀損。因此,這些字元必須在 <a> 標記或查詢字串中編碼,而查詢字串中的字串可能是瀏覽器以要求字串所重新傳送。
UrlPathEncode 是在執行階段從 ASP.Net 應用程式存取 HttpUtility..::.UrlPathEncode 方法的一種很方便的方式。在內部,UrlPathEncode 使用 HttpUtility..::.UrlPathEncode 為字串編碼。
下列範例會將字串以 URL 編碼,然後傳送到瀏覽器用戶端。在這個範例中,字串 MyURL 會編碼成 "http%3a%2f%2fwww.contoso.com%2farticles.aspx?title = ASP.NET Examples"。
Dim MyURL As String
MyURL = "http://www.contoso.com/articles.aspx?title = ASP.NET Examples"
Response.Write( "<A HREF = " & Server.UrlPathEncode(MyURL) & "> ASP.NET Examples <br>")
String MyURL;
MyURL = "http://www.contoso.com/articles.aspx?title = ASP.NET Examples";
Response.Write( "<A HREF = " + Server.UrlPathEncode(MyURL) + "> ASP.NET Examples <br>" );
String myURL;
myURL = "http://www.contoso.com/articles.aspx?title = ASP.NET Examples";
get_Response().Write(("<A HREF = " + get_Server().UrlPathEncode(myURL)
+ "> ASP.NET Examples <br>"));
var myURL : String
myURL = "http://www.contoso.com/articles.aspx?title = ASP.NET Examples"
Response.Write( "<A HREF = " + Server.UrlPathEncode(myURL) + "> ASP.NET Examples <br>")
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
參考