Posts
8
Comments
25
Trackbacks
0
A more useful UrlBuilder class

When I started my first ASP.NET project in 05, my URL building code included way too many string concatenations, String.Format calls and plain literals.

Being pre-MS-MVC days of typed URL construction via routing engines, I needed some code to make URL construction (particularly the query string) easier.

Then I found this UrlBuilder class. This is a great little class because it neatly derives from the framework UriBuilder class.

Since I started using it, I found it was inadequate when dealing with case sensitive HTTP servers. The specialized StringDictionary it uses lower-cases all keys and values.

To suit my needs, I modified the class in a few ways:

  • Upgraded to C# 3 - I think I'm addicted to lambda's so I involuntarily compiled it in .NET framework 3.5
  • The un-rendered query string is now stored as Dictionary<string,string> instead of StringDictionary which avoids casing problems
  • Provided overloads for ToString() casing - upper,lower or default (leaves casing alone)

Here's a simple example for building a Clickatell SMS sending URL.

UrlBuilder builder = new UrlBuilder("http://api.clickatell.com/http/sendmsg");
builder.QueryString["api_id"] = "23223";
builder.QueryString["user"] = "cutnpaste";
builder.QueryString["password"] = "ninja";
builder.QueryString["to"] = "61043678945";
builder.QueryString["text"] = "the message to send";
Console.WriteLine(builder.ToString(StringCaseDirection.Lower));

Download source code

kick it on DotNetKicks.com

posted on Wednesday, January 02, 2008 4:33 AM Print
Comments
Gravatar
# re: A more useful UrlBuilder class
Chris Pietschmann
1/9/2008 7:37 AM
A generic dictionary isn't the best collection class to use to store the querystring parameters. By using Dictionary, you are limiting the ability to only pass one value per querystring parameter, and there are plenty of time when you may need to pass in multiple. Like this: http://site/page.aspx?name=chris&name=john
Gravatar
# re: A more useful UrlBuilder class
Chris Pietschmann
1/26/2008 3:01 AM
The best implementation would probably be a custom dictionary that can accept a string or a List<string>. And then under the hood it converts the single string to List<string>. Its usage could be like this:

builder.QueryString.Add("key", "value1");
builder.QueryString.Add("key", "value2");

List<string> myList = new List<string>();
builder.QueryString.Add("key", myList);

And then using the default indexer of the dictionary could override that keys value with what ever you set it to. Again accepting either string or List<string>, and then converting string to List<string> under the hood. User would be like this:

builder.QueryString["key"] = "value3";

OR

List<string> myList = new List<string>();
builder.QueryString["key"] = myList;

This way the dictionary is really flexible when adding a single or multiple values to the same querystring key.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 5 and 8 and type the answer here: