Best way to convert query string to dictionary in C#
HttpUtility.ParseQueryString()
parses query string into a NameValueCollection
object, converting the latter to an IDictionary<string, string>
is a matter of a simple foreach
. This, however, might be unnecessary since NameValueCollection
has an indexer, so it behaves pretty much like a dictionary.
Here is how I usually do it
Dictionary<string, string> parameters = HttpContext.Current.Request.QueryString.Keys.Cast<string>()
.ToDictionary(k => k, v => HttpContext.Current.Request.QueryString[v]);