using System.Collections.Generic;
var tests = new Dictionary<string, string>()
{ "http://foo.com", "http://foo.com?a=b" },
{ "http://foo.com/", "http://foo.com/?a=b" },
{ "http://foo.com/#", "http://foo.com/#?a=b" },
{ "http://foo.com/#/", "http://foo.com/#/?a=b" },
{ "http://foo.com/#!/", "http://foo.com/#!/?a=b" },
{ "http://foo.com/#frag", "http://foo.com/?a=b#frag" },
{ "http://foo.com/#frag/", "http://foo.com/?a=b#frag/" },
{ "http://foo.com/?query", "http://foo.com/?query&a=b" },
{ "http://foo.com/?query#frag", "http://foo.com/?query&a=b#frag" },
{ "http://foo.com/?a=x", "http://foo.com/?a=x&a=b" }
var methods = new Func<string, string, string, string>[] {
AddParameterWithUriBuilder
foreach (var m in methods) {
Console.WriteLine("----- " + m.Method.Name + " -----");
foreach (var s in tests) {
var r = AddParameterWithUriBuilder(s.Key, "a", "b");
if (r.ToString() == s.Value) {
Console.WriteLine("OK '" + s.Key + "' > '" + s.Value + "'");
Console.WriteLine("FAIL '" + s.Key + "' > '" + r.ToString() + "', expected: '" + s.Value + "'");
public string AddParameterWithUriBuilder(string url, string paramName, string paramValue)
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[paramName] = paramValue;
uriBuilder.Query = query.ToString();
return uriBuilder.Uri.ToString();
public static string AddParameterWithUriBuild(string url, string paramName, string paramValue)
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[paramName] = paramValue;
uriBuilder.Query = query.ToString();
return uriBuilder.Uri.ToString();