Last active
December 25, 2015 02:49
-
-
Save ismaelhamed/6905335 to your computer and use it in GitHub Desktop.
Some extension methods for the object NameValueCollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System.Collections.Specialized | |
{ | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
public static class NameValueCollectionExtensions | |
{ | |
public static IDictionary<string, string> ToDictionary(this NameValueCollection source) | |
{ | |
return source.Cast<string>().Select(s => new { Key = s, Value = source[s] }).ToDictionary(p => p.Key, p => p.Value); | |
} | |
public static string ToQueryString(this NameValueCollection source) | |
{ | |
return string.Concat("?", string.Join("&", ArrayEx.ConvertAll(source.AllKeys.ToArray(), key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(source[key]))))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment