Project Description
Provides a utility class for building URL's for use in any application. It uses a fluent syntax.
It has support for adding parts to the URL (e.g. subfolders) and query string.
The main advantage is not having to use the "/" slash character, and traceability of URL creation.

Examples

Simple example of building a URL.
UrlBuilder ub = new UrlBuilder("http://www.google.com")
    .AddPart("int")
    .AddPart("en")
    .AddPart("ads");
Console.WriteLine(ub.ToString());
Results in:
http://www.google.com/int/en/ads

A more complex example (with query string):
UrlBuilder ub = new UrlBuilder("www.somesite.com///")
    .AddPart("//news/")
    .AddPart("recent/")
    .AddPart("/local")
    .AddPart("/item.aspx//")
    .AddQueryString("ID", "kjhnb3");
Console.WriteLine(ub.ToString());
Results in:
www.somesite.com/news/recent/local/item.aspx?ID=kjhnb3

A less friendly example. Notice the order of calls, the empty query string parameter that's ignored and the empty part.
UrlBuilder ub = new UrlBuilder("www.somesite.com///")
    .AddPart("//news/")
    .AddQueryString("ID", "kjhnb3")
    .AddPart("recent/")
    .AddPart("/local")
    .AddPart("")
    .AddPart("/item.aspx//")
    .AddQueryString("WAT?", "");
Console.WriteLine(ub.ToString());
Results in:
www.somesite.com/news/recent/local/item.aspx?ID=kjhnb3

These examples only highlight the basic scenarios. The most advantages are of course with dynamically built URLs which come from business logic.
If you need to find all locations where URLs are built, for example because you made a booboo and forgot to generalize your base URL, you can now easily find it, which is a lot better than searching your entire solution for "/"...

Last edited Dec 22, 2011 at 8:37 AM by SabbeRubbish, version 11