Returns a querystring based upon the values in a IDictionary. This will not return the ? character. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Returns a querystring based upon the values in a IDictionary. This will not return the ? character.

Demo Code


using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;/*  w  w  w  . ja  v a2 s  .co  m*/

public class Main{
        /// <summary>
        /// Returns a querystring based upon the values in a IDictionary. This will not return the ? character.
        /// </summary>
        /// <param name="value">The NameValueCollection</param>
        /// <param name="ignoreKeys">The keys to ignore. These will be treated case-insensitively.</param>
        public static string ToQueryString(this IDictionary<string, string> value, string[] ignoreKeys)
        {
            var lowerCaseKeys = ignoreKeys == null ? new string[] { } : ignoreKeys.Where(x => !String.IsNullOrEmpty(x)).Select(x => x.ToLower());

            return HttpUtility.UrlDecode(
                String.Join("&",
                    Array.ConvertAll(
                        value.Keys.Where(x => !lowerCaseKeys.Contains(x.ToLower())).ToArray(),
                        key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value[key]))
                    )
                )
            );
        }
}

Related Tutorials