如何使用 C# 解码 URL 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1405048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:09:35 来源:igfitidea点击:
How do I decode a URL parameter using C#?
提问by Tom
How can I decode an encoded URL parameter using C#?
如何使用 C# 解码编码的 URL 参数?
For example, take this URL:
例如,使用这个 URL:
my.aspx?val=%2Fxyz2F
采纳答案by TheVillageIdiot
Server.UrlDecode(xxxxxxxx)
回答by Jon Skeet
Have you tried HttpServerUtility.UrlDecode
or HttpUtility.UrlDecode
?
回答by Canavar
Try this:
尝试这个:
string decodedUrl = HttpUtility.UrlDecode("my.aspx?val=%2Fxyz2F");
回答by ogi
string decodedUrl = Uri.UnescapeDataString(url)
or
或者
string decodedUrl = HttpUtility.UrlDecode(url)
Url is not fully decoded with one call. To fully decode you can call one of this methods in a loop:
Url 无法通过一次调用完全解码。要完全解码,您可以在循环中调用以下方法之一:
private static string DecodeUrlString(string url) {
string newUrl;
while ((newUrl = Uri.UnescapeDataString(url)) != url)
url = newUrl;
return newUrl;
}
回答by Matheus Miranda
Try:
尝试:
var myUrl = "my.aspx?val=%2Fxyz2F";
var decodeUrl = System.Uri.UnescapeDataString(myUrl);