This question already has an answer here:
这个问题已经有了答案:
- Get url parameters from a string in .NET 11 answers
- 从。net 11中的字符串获取url参数。
I have a uri string like: http://example.com/file?a=1&b=2&c=string%20param
我有一个uri字符串:http://example.com/file?
Is there an existing function that would convert query parameter string into a dictionary same way as ASP.NET Context.Request does it.
是否有一个函数可以像ASP一样将查询参数字符串转换成字典。净上下文。请求它。
I'm writing a console app and not a web-service so there is no Context.Request to parse the URL for me.
我正在编写一个控制台应用程序,而不是web服务,所以没有上下文。请求为我解析URL。
I know that it's pretty easy to crack the query string myself but I'd rather use a FCL function is if exists.
我知道自己很容易破解查询字符串,但我宁愿使用FCL函数if存在。
10 个解决方案
#1
81
Use this:
用这个:
string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
This code by Tejs isn't the 'proper' way to get the query string from the URI:
Tejs编写的这段代码不是从URI获取查询字符串的“正确”方法:
string.Join(string.Empty, uri.Split('?').Skip(1));
#2
113
You can use:
您可以使用:
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
MSDN
#3
25
This should work:
这应该工作:
string url = "http://example.com/file?a=1&b=2&c=string%20param";
string querystring = url.Substring(url.IndexOf('?'));
System.Collections.Specialized.NameValueCollection parameters =
System.Web.HttpUtility.ParseQueryString(querystring);
According to MSDN. Not the exact collectiontype you are looking for, but nevertheless useful.
根据MSDN。不是你要找的那种,但是很有用。
Edit: Apparently, if you supply the complete url to ParseQueryString
it will add 'http://example.com/file?a' as the first key of the collection. Since that is probably not what you want, I added the substring to get only the relevant part of the url.
编辑:显然,如果您向ParseQueryString提供完整的url,它会添加“http://example.com/file?”a'作为集合的第一个键。因为这可能不是您想要的,所以我添加了substring来获取url的相关部分。
#4
14
I had to do this for a modern windows app. I used the following:
我必须为一个现代的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;
}
}
#5
12
Have a look at HttpUtility.ParseQueryString() It'll give you a NameValueCollection
instead of a dictionary, but should still do what you need.
parsequerystring()将提供一个NameValueCollection,而不是dictionary,但仍然应该执行所需的操作。
The other option is to use string.Split()
.
另一个选项是使用string.Split()。
string url = @"http://example.com/file?a=1&b=2&c=string%20param";
string[] parts = url.Split(new char[] {'?','&'});
///parts[0] now contains http://example.com/file
///parts[1] = "a=1"
///parts[2] = "b=2"
///parts[3] = "c=string%20param"
#6
5
For isolated projects, where dependencies must be kept to a minimum, I found myself using this implementation:
对于孤立的项目,依赖项必须保持在最低限度,我发现自己使用了这个实现:
var arguments = uri.Query
.Substring(1) // Remove '?'
.Split('&')
.Select(q => q.Split('='))
.ToDictionary(q => q.FirstOrDefault(), q => q.Skip(1).FirstOrDefault());
Due note though, that I do not handle encoded strings of any kind, as I was using this in a controlled environment, where encoding issues would be a coding error on the server side that should be fixed.
但是,要注意的是,我没有处理任何类型的编码字符串,因为我在一个受控的环境中使用这个,在这个环境中编码问题将是服务器端的编码错误,应该是固定的。
#7
4
In a single line of code:
用一行代码:
string xyz = Uri.UnescapeDataString(HttpUtility.ParseQueryString(Request.QueryString.ToString()).Get("XYZ"));
#8
0
Below is the link for your reference to use HttpUtility class to parse querystring values: http://dnohr.dk/aspnet/how-to-get-url-querystring-variables-from-a-string-url-with-csharp
下面是使用HttpUtility类解析querystring值的链接:http://dnohr.dk/aspnet/how- get-url-querystring-变量-from-a-string-url-with-csharp
#9
0
Microsoft Azure offers a framework that makes it easy to perform this. http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html#//api/name/readWithQueryString:completion:
微软Azure提供了一种框架,可以很容易地实现这一点。http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html / / api /名称/ readWithQueryString:完成:
#10
-2
You could reference System.Web in your console application and then look for the Utility functions that split the URL parameters.
你可以参考系统。在控制台应用程序中创建Web,然后查找拆分URL参数的实用函数。
#1
81
Use this:
用这个:
string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
This code by Tejs isn't the 'proper' way to get the query string from the URI:
Tejs编写的这段代码不是从URI获取查询字符串的“正确”方法:
string.Join(string.Empty, uri.Split('?').Skip(1));
#2
113
You can use:
您可以使用:
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
MSDN
#3
25
This should work:
这应该工作:
string url = "http://example.com/file?a=1&b=2&c=string%20param";
string querystring = url.Substring(url.IndexOf('?'));
System.Collections.Specialized.NameValueCollection parameters =
System.Web.HttpUtility.ParseQueryString(querystring);
According to MSDN. Not the exact collectiontype you are looking for, but nevertheless useful.
根据MSDN。不是你要找的那种,但是很有用。
Edit: Apparently, if you supply the complete url to ParseQueryString
it will add 'http://example.com/file?a' as the first key of the collection. Since that is probably not what you want, I added the substring to get only the relevant part of the url.
编辑:显然,如果您向ParseQueryString提供完整的url,它会添加“http://example.com/file?”a'作为集合的第一个键。因为这可能不是您想要的,所以我添加了substring来获取url的相关部分。
#4
14
I had to do this for a modern windows app. I used the following:
我必须为一个现代的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;
}
}
#5
12
Have a look at HttpUtility.ParseQueryString() It'll give you a NameValueCollection
instead of a dictionary, but should still do what you need.
parsequerystring()将提供一个NameValueCollection,而不是dictionary,但仍然应该执行所需的操作。
The other option is to use string.Split()
.
另一个选项是使用string.Split()。
string url = @"http://example.com/file?a=1&b=2&c=string%20param";
string[] parts = url.Split(new char[] {'?','&'});
///parts[0] now contains http://example.com/file
///parts[1] = "a=1"
///parts[2] = "b=2"
///parts[3] = "c=string%20param"
#6
5
For isolated projects, where dependencies must be kept to a minimum, I found myself using this implementation:
对于孤立的项目,依赖项必须保持在最低限度,我发现自己使用了这个实现:
var arguments = uri.Query
.Substring(1) // Remove '?'
.Split('&')
.Select(q => q.Split('='))
.ToDictionary(q => q.FirstOrDefault(), q => q.Skip(1).FirstOrDefault());
Due note though, that I do not handle encoded strings of any kind, as I was using this in a controlled environment, where encoding issues would be a coding error on the server side that should be fixed.
但是,要注意的是,我没有处理任何类型的编码字符串,因为我在一个受控的环境中使用这个,在这个环境中编码问题将是服务器端的编码错误,应该是固定的。
#7
4
In a single line of code:
用一行代码:
string xyz = Uri.UnescapeDataString(HttpUtility.ParseQueryString(Request.QueryString.ToString()).Get("XYZ"));
#8
0
Below is the link for your reference to use HttpUtility class to parse querystring values: http://dnohr.dk/aspnet/how-to-get-url-querystring-variables-from-a-string-url-with-csharp
下面是使用HttpUtility类解析querystring值的链接:http://dnohr.dk/aspnet/how- get-url-querystring-变量-from-a-string-url-with-csharp
#9
0
Microsoft Azure offers a framework that makes it easy to perform this. http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html#//api/name/readWithQueryString:completion:
微软Azure提供了一种框架,可以很容易地实现这一点。http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html / / api /名称/ readWithQueryString:完成:
#10
-2
You could reference System.Web in your console application and then look for the Utility functions that split the URL parameters.
你可以参考系统。在控制台应用程序中创建Web,然后查找拆分URL参数的实用函数。