Esempio n. 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);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs a Url object from a string.
        /// </summary>
        /// <param name="baseUrl">The URL to use as a starting point (required)</param>
        public Url(string baseUrl)
        {
            if (baseUrl == null)
            {
                throw new ArgumentNullException("baseUrl");
            }

            var parts = baseUrl.Split('?');

            Path        = parts[0];
            QueryParams = QueryParamCollection.Parse(parts.Length > 1 ? parts[1] : "");
        }
Esempio n. 3
0
File: Url.cs Progetto: hzl091/Flurl
        /// <summary>
        /// Parses a URL query to a QueryParamCollection dictionary.
        /// </summary>
        /// <param name="query">The URL query to parse.</param>
        public static QueryParamCollection ParseQueryParams(string query)
        {
            var result = new QueryParamCollection();

            query = query?.TrimStart('?');
            if (string.IsNullOrEmpty(query))
            {
                return(result);
            }

            result.AddRange(
                from p in query.Split('&')
                let pair                                                           = p.SplitOnFirstOccurence('=')
                                                  let name                         = pair[0]
                                                                         let value = (pair.Length == 1) ? null : pair[1]
                                                                                     select new QueryParameter(name, value, true));

            return(result);
        }
Esempio n. 4
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);
        }