Constructs Uri query string by name value collection. - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

Constructs Uri query string by name value collection.

Demo Code


using System.Web;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;
using System;/*from  w  w  w  .j av  a 2s.c  o m*/

public class Main{
        /// <summary>
        /// Constructs query string by name value collection.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        public static string ConstructQueryString(NameValueCollection parameters)
        {
            var sb = new StringBuilder();

            foreach (String name in parameters)
                sb.Append(String.Concat(name, "=", System.Web.HttpUtility.UrlEncode(parameters[name]), "&"));

            if (sb.Length > 0)
                return sb.ToString(0, sb.Length - 1);

            return String.Empty;
        }
}

Related Tutorials