Converts a dictionary instance into a url encoded querystring - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Converts a dictionary instance into a url encoded querystring

Demo Code


using System.Web;
using System.Linq;
using System.Collections.Generic;

public class Main{
        /// <summary>
        /// Converts a dictionary instance into a url encoded querystring
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <returns></returns>
        public static string ToQueryString(this IDictionary<string, string> dictionary)
        {/*from   ww  w.ja  v  a2s  . co m*/
            if (dictionary == null || dictionary.Count <= 0)
                return string.Empty;

            var parameters = dictionary
                    .Select(keyValuePair => string.Format("{0}={1}", keyValuePair.Key, HttpUtility.UrlEncode(keyValuePair.Value)));

            return string.Join("&", parameters);
        }
}

Related Tutorials