的區別是串的一個編碼地址和一個編碼路徑部分(這意味着URL的查詢串之前的部分)從地址,在這裏是如何看起來實現:
/// <summary>Encodes a URL string.</summary>
/// <returns>An encoded string.</returns>
/// <param name="str">The text to encode. </param>
public static string UrlEncode(string str)
{
if (str == null)
{
return null;
}
return HttpUtility.UrlEncode(str, Encoding.UTF8);
}
這裏是實現UrlPathEncode的:
/// <summary>Encodes the path portion of a URL string for reliable HTTP transmission from the Web server to a client.</summary>
/// <returns>The URL-encoded text.</returns>
/// <param name="str">The text to URL-encode. </param>
public static string UrlPathEncode(string str)
{
if (str == null)
{
return null;
}
int num = str.IndexOf('?'); // <--- notice this
if (num >= 0)
{
return HttpUtility.UrlPathEncode(str.Substring(0, num)) + str.Substring(num);
}
return HttpUtility.UrlEncodeSpaces(HttpUtility.UrlEncodeNonAscii(str, Encoding.UTF8));
}
和MSDN還規定了HttpUtility.UrlEnocde
:
這些方法重載可用於編碼整個URL,包括查詢字符串值。
的可能的複製[HttpServerUtility.UrlPathEncode VS HttpServerUtility.UrlEncode](http://stackoverflow.com/questions/4145823/httpserverutility-urlpathencode-vs-httpserverutility-urlencode) – avs099
HTTP的可能的複製:// stackoverflow.com/questions/7997934/in-asp-net-why-is-there-urlencode-and-urlpathencode – bodman