Friday, October 14, 2016

Building a url with a querystring and encoding values in Url

Encoding/Decoding query parameter values

using System.Web.HttpUtility;

HttpUtility.UrlEncode(s);

HttpUtility.UrlDecode(string str);

Building a url with query string

var queryParms = HttpUtility.ParseQueryString(String.Empty);
queryParms.Add("q", HttpUtility.UrlEncode("books,magazines"));
queryParms.Add("numResults", 1);
UriBuilder ub = new UriBuilder("http://somesite.com/");
ub.Query = queryParms.ToString();
return ub.Uri;