如何使用 C # 解码编码的 URL 参数?
例如,以这个 URL 为例:
my.aspx?val=%2Fxyz2F
你试过 HttpServerUtility.UrlDecode或 HttpUtility.UrlDecode吗?
HttpServerUtility.UrlDecode
HttpUtility.UrlDecode
Server.UrlDecode(xxxxxxxx)
试试这个:
string decodedUrl = HttpUtility.UrlDecode("my.aspx?val=%2Fxyz2F");
string decodedUrl = Uri.UnescapeDataString(url)
或者
string decodedUrl = HttpUtility.UrlDecode(url)
一次调用不能完全解码 Url。要完全解码,您可以在循环中调用下列方法之一:
private static string DecodeUrlString(string url) { string newUrl; while ((newUrl = Uri.UnescapeDataString(url)) != url) url = newUrl; return newUrl; }
试试:
var myUrl = "my.aspx?val=%2Fxyz2F"; var decodeUrl = System.Uri.UnescapeDataString(myUrl);