/// <summary> /// Parses a query string from a URL to a QueryParamCollection dictionary. /// </summary> /// <param name="queryString">The query string to parse.</param> /// <returns></returns> public static QueryParamCollection Parse(string queryString) { var result = new QueryParamCollection(); if (string.IsNullOrEmpty(queryString)) { return(result); } queryString = queryString.TrimStart('?').Split('?').Last(); var pairs = ( from kv in queryString.Split('&') let pair = kv.Split('=') let key = pair[0] let value = pair.Length >= 2 ? UrlBuilder.DecodeQueryParamValue(pair[1]) : "" group value by key into g select new { g.Key, Values = g.ToArray() }); foreach (var g in pairs) { if (g.Values.Length == 1) { result.Add(g.Key, g.Values[0]); } else { result.Add(g.Key, g.Values); } } return(result); }