How to get a list of all URL parameters with their values ​​in asp.net c #?

In my asp.net C # solution, I want to get a dictionary of all url parameters, where key is the parameter name and value is the parameter value. How can i do this?

Thanks.

+4
source share
3 answers

You need HttpUtility.ParseQueryString

NameValueCollection qscollection = HttpUtility.ParseQueryString(querystring); 

you can use this to get the querystring value:

  Request.Url.Query 

Match together

 NameValueCollection qscollection = HttpUtility.ParseQueryString(Request.Url.Query); 

Additional information at http://msdn.microsoft.com/en-us/library/ms150046.aspx

A tighter manual way without using the built-in method is as follows:

 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); } } 
+15
source

The HttpRequest.QueryString object is already a collection. Basically, the name is ValueCollection. Check here for a more detailed explanation on MSDN. To convert this collection to a dictionary, you can add an extension method as follows.

  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 just do the following, since Request.QueryString is basically a NameValueCollection

 IDictionary<string,string> dc = Request.QueryString.ToDictionary(); 
+2
source

I often run into the same problem. I always do it like this:

 Dictionary<string, string> allRequestParamsDictionary = Request.Params.AllKeys.ToDictionary(x => x, x => Request.Params[x]); 

This gives all the parameters for the query, including the variables QueryString, Body, ServerVariables and Cookie. For QueryString only, do the following:

 Dictionary<string, string> queryStringDictionary = Request.QueryString.AllKeys.ToDictionary(x => x, x => Request.Params[x]); 
0
source

Source: https://habr.com/ru/post/1487395/


All Articles