Example #1
0
        /// <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('?')[0];

            var pairs = (
                from kv in queryString.Split('&')
                let pair = kv.Split('=')
                           let key = pair[0]
                                     let value = Uri.UnescapeDataString(pair.Length >= 2 ? pair[1] : "")
                                                 group value by key into g
                                                 select new { Key = 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);
        }
Example #2
0
        /// <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('?')[0];

            foreach (var kv in queryString.Split('&'))
            {
                var pair  = kv.Split('=');
                var key   = pair[0];
                var value = pair.Length > 0 ? pair[1] : "";
                result.Add(key, Uri.UnescapeDataString(value));
            }

            return(result);
        }