我有一个uri字符串,比如:http://example.com/file?a=1&b=2&c=string%20param
是否有一个现有的函数可以将查询参数字符串转换为字典,方法与ASP.NET Context.Request相同。
我正在编写一个控制台应用程序,而不是web服务,所以没有Context.Request为我解析网址。
我知道我自己很容易破解查询字符串,但我宁愿使用FCL函数。
发布于 2013-05-08 04:02:45
使用以下命令:
string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
Tejs的这段代码不是从URI获取查询字符串的“正确”方式:
string.Join(string.Empty, uri.Split('?').Skip(1));
发布于 2010-05-22 02:27:29
您可以使用:
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
发布于 2014-02-26 23:48:23
为了一个现代的windows应用,我不得不这么做。我使用了以下代码:
public static class UriExtensions
{
private static readonly Regex _regex = new Regex(@"[?&](\w[\w.]*)=([^?&]+)");
public static IReadOnlyDictionary<string, string> ParseQueryString(this Uri uri)
{
var match = _regex.Match(uri.PathAndQuery);
var paramaters = new Dictionary<string, string>();
while (match.Success)
{
paramaters.Add(match.Groups[1].Value, match.Groups[2].Value);
match = match.NextMatch();
}
return paramaters;
}
}
https://stackoverflow.com/questions/2884551
复制相似问题