Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

09 Aug 2008

Presenting UriHelper

Adding parameters to an Uri is an example where my NameValueCollectionHelper comes in handy. Although UriTemplate allows us to bind parameters, it doesn’t really support add/remove/fail on duplicate parameters. Here is an example

[TestMethod]
public void TestAddParametersReplaceWithReplaceOfExistingParameters()
{
	Uri originalUri = new Uri("http://www.example.com/path/?key1=val1&key2=val2#abcd")

	NameValueCollection parameters = new NameValueCollection();
	parameters.Add("key1", "newval");
	parameters.Add("key3", "val3");

	Uri expected = new Uri("http://www.example.com/path/?key1=newval&key2=val2&key3=val3#abcd");

	Uri actual = UriHelper.AddParameters(originalUri, parameters, DuplicateKeyBehavior.Replace);
	Assert.AreEqual(expected, actual);
}

Once again, source of this class can be found at BeTimvwFramework.