如何在asp.net c#中获取所有url参数及其值的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17226275/
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
How to get a list of all the url parameters with their values in asp.net c#?
提问by omega
In my asp.net c# solution, I want to get a dictionary of all the url parameters, where the key is the parameter name and the value is the parameter value. How can I do this?
在我的 asp.net c# 解决方案中,我想获取所有 url 参数的字典,其中键是参数名称,值是参数值。我怎样才能做到这一点?
Thanks.
谢谢。
采纳答案by ericdc
You need HttpUtility.ParseQueryString
你需要 HttpUtility.ParseQueryString
NameValueCollection qscollection = HttpUtility.ParseQueryString(querystring);
you can use this to get the querystring value itself:
您可以使用它来获取查询字符串值本身:
Request.Url.Query
To put it together
把它放在一起
NameValueCollection qscollection = HttpUtility.ParseQueryString(Request.Url.Query);
More info at http://msdn.microsoft.com/en-us/library/ms150046.aspx
更多信息请访问http://msdn.microsoft.com/en-us/library/ms150046.aspx
The harder manual way without using the builtin method is this:
不使用内置方法的更难的手动方法是:
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split('&');
foreach(string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
queryParameters.Add(key, val);
}
}
回答by Rajaraam Murali
The HttpRequest.QueryStringobject is already a collection. Basically a NameValueCollection. Check herefor a more detailed explanation on MSDN. To convert this collection in to a dictionary you could add an extension method as follows.
该HttpRequest.QueryString对象已经是一个集合。基本上是一个 NameValueCollection。请在这里在MSDN上更详细的解释。要将此集合转换为字典,您可以添加如下扩展方法。
public static class NameValueCollectionExtension
{
public static IDictionary<string, string> ToDictionary(this NameValueCollection sourceCollection)
{
return sourceCollection.Cast<string>()
.Select(i => new { Key = i, Value = sourceCollection[i] })
.ToDictionary(p => p.Key, p => p.Value);
}
}
Then you could basically just do the following since Request.QueryString is basically a NameValueCollection
然后你基本上可以做以下,因为 Request.QueryString 基本上是一个 NameValueCollection
IDictionary<string,string> dc = Request.QueryString.ToDictionary();
回答by Emil Ingerslev
I often get into the same problem. I always end up doing it like this:
我经常遇到同样的问题。我总是这样做:
Dictionary<string, string> allRequestParamsDictionary = Request.Params.AllKeys.ToDictionary(x => x, x => Request.Params[x]);
This gives all params for the request, including QueryString, Body, ServerVariables and Cookie variables. For only QueryString do this:
这给出了请求的所有参数,包括 QueryString、Body、ServerVariables 和 Cookie 变量。只有 QueryString 这样做:
Dictionary<string, string> queryStringDictionary = Request.QueryString.AllKeys.ToDictionary(x => x, x => Request.Params[x]);

