Add Query String to Uri - CSharp System.Net

CSharp examples for System.Net:IP Address

Description

Add Query String to Uri

Demo Code


using System.Web;
using System.Text;
using System.Collections.Specialized;
using System;/*w w  w.j av a 2 s. co m*/

public class Main{
        public static Uri AddQueryString(this Uri uri, NameValueCollection collection)
        {
            StringBuilder sb = new StringBuilder(uri.AbsoluteUri);
            sb.Append("?");
            sb.Append(string.Join("&", Array.ConvertAll(collection.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(collection[key])))));

            uri = new Uri(sb.ToString());
            return uri;
        }
}

Related Tutorials