Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>HttpUtility.ParseQueryString</code> returns <code>HttpValueCollection</code> (Internal Class) which inherits from <code>NameValueCollection</code>. <code>NameValueCollection</code> is a collection of key value pairs like a dictionary but it supports duplicates, maintains order and only implements <code>IEnumerable</code> (This collection is pre-generics). <code>NameValueCollection</code> is not supported in PCL.</p> <p>My solution (Partly lifted and modified from the .NET framework) is to substitute HttpValueCollection with <code>Collection&lt;HttpValue&gt;</code> where <code>HttpValue</code> is just a key value pair. </p> <pre><code>public sealed class HttpUtility { public static HttpValueCollection ParseQueryString(string query) { if (query == null) { throw new ArgumentNullException("query"); } if ((query.Length &gt; 0) &amp;&amp; (query[0] == '?')) { query = query.Substring(1); } return new HttpValueCollection(query, true); } } public sealed class HttpValue { public HttpValue() { } public HttpValue(string key, string value) { this.Key = key; this.Value = value; } public string Key { get; set; } public string Value { get; set; } } public class HttpValueCollection : Collection&lt;HttpValue&gt; { #region Constructors public HttpValueCollection() { } public HttpValueCollection(string query) : this(query, true) { } public HttpValueCollection(string query, bool urlencoded) { if (!string.IsNullOrEmpty(query)) { this.FillFromString(query, urlencoded); } } #endregion #region Parameters public string this[string key] { get { return this.First(x =&gt; string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase)).Value; } set { this.First(x =&gt; string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase)).Value = value; } } #endregion #region Public Methods public void Add(string key, string value) { this.Add(new HttpValue(key, value)); } public bool ContainsKey(string key) { return this.Any(x =&gt; string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase)); } public string[] GetValues(string key) { return this.Where(x =&gt; string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase)).Select(x =&gt; x.Value).ToArray(); } public void Remove(string key) { this.Where(x =&gt; string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase)) .ToList() .ForEach(x =&gt; this.Remove(x)); } public override string ToString() { return this.ToString(true); } public virtual string ToString(bool urlencoded) { return this.ToString(urlencoded, null); } public virtual string ToString(bool urlencoded, IDictionary excludeKeys) { if (this.Count == 0) { return string.Empty; } StringBuilder stringBuilder = new StringBuilder(); foreach (HttpValue item in this) { string key = item.Key; if ((excludeKeys == null) || !excludeKeys.Contains(key)) { string value = item.Value; if (urlencoded) { // If .NET 4.5 and above (Thanks @Paya) key = WebUtility.UrlDecode(key); // If .NET 4.0 use this instead. // key = Uri.EscapeDataString(key); } if (stringBuilder.Length &gt; 0) { stringBuilder.Append('&amp;'); } stringBuilder.Append((key != null) ? (key + "=") : string.Empty); if ((value != null) &amp;&amp; (value.Length &gt; 0)) { if (urlencoded) { value = Uri.EscapeDataString(value); } stringBuilder.Append(value); } } } return stringBuilder.ToString(); } #endregion #region Private Methods private void FillFromString(string query, bool urlencoded) { int num = (query != null) ? query.Length : 0; for (int i = 0; i &lt; num; i++) { int startIndex = i; int num4 = -1; while (i &lt; num) { char ch = query[i]; if (ch == '=') { if (num4 &lt; 0) { num4 = i; } } else if (ch == '&amp;') { break; } i++; } string str = null; string str2 = null; if (num4 &gt;= 0) { str = query.Substring(startIndex, num4 - startIndex); str2 = query.Substring(num4 + 1, (i - num4) - 1); } else { str2 = query.Substring(startIndex, i - startIndex); } if (urlencoded) { this.Add(Uri.UnescapeDataString(str), Uri.UnescapeDataString(str2)); } else { this.Add(str, str2); } if ((i == (num - 1)) &amp;&amp; (query[i] == '&amp;')) { this.Add(null, string.Empty); } } } #endregion } </code></pre> <p><strong>UPDATE</strong></p> <p>Updated so that HttpValueCollection now inherits from Collection rather than List as highlighted in the comments.</p> <p><strong>UPDATE 2</strong></p> <p>Updated to use WebUtility.UrlDecode if using .NET 4.5, thanks to @Paya.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload