Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET
Article

A useful UrlBuilder class

Rate me:
Please Sign up or sign in to vote.
4.70/5 (38 votes)
18 Aug 20053 min read 157.9K   1.9K   102   34
A simple way to generate and manipulate URLs.

Introduction

Building URLs is something one does all the time as an ASP.NET developer. The .NET Framework has generously provided many classes to help us simplify the problem, but after all my searching, the one thing I found missing from the framework was a way to easily work with QueryString parameters. I did a little looking around and found a few solutions to the problem written by various people but none of them built on anything already in the framework. The framework provides the very useful System.UriBuilder class but it has no in-built way to deal with QueryString parameters apart from being able to get back a string containing the Query portion of the URI. While this is not all that useful in itself, it does help us create a derived class which extends the functionality in UriBuilder. Enter UrlBuilder. While UriBuilder is a more accurate name given modern naming conventions, UrlBuilder is semantically equivalent and more importantly, not a duplicate name of anything already in the framework.

If the reader wishes to get elbow deep in the code to discover its inner workings, it is provided in the download for your perusal and should be immediately obvious how it all hangs together. All I shall attempt to do here is give a brief explanation of how to use the UrlBuilder class by means of some sample code which will hopefully outline and highlight the salient features of this class. So then, without further delay, let's delve in.

The UrlBuilder class can be instantiated in several ways, but for simplicity, let's assume the user wishes to use a string as the initialiser of this UrlBuilder instance.

C#
UrlBuilder builder = new 
    UrlBuilder("http://www.codeproject.com/index.asp?cat=4");

The rest of the constructor overloads provided in the class are exactly the same as those of the base UriBuilder class with the exception of one additional which takes a System.Web.UI.Page object. This is useful when one wishes to merely redirect to the same page with different QueryString parameters. The object can also be initialised using a System.Uri object. Once instantiated, we can manipulate any part of the URI (see the System.Uri documentation).

C#
builder.Host = "www.gibbons.co.za";
builder.Path = "archive/2005";

It is interesting to note that a forward slash “/” is optional at the start of the Path string. Either way, the UriBuilder class will return a correctly formed URI. The UriBuilder class provides no way to set only the page name portion of the URI, so I added a new property called PageName which allows you to set only the name of the required page.

C#
builder.PageName = "06.aspx";

So far, apart from the PageName property, this is nothing new; all this can already be done with the UriBuilder class. The real usefulness of the UrlBuilder class becomes apparent when we try to manipulate the QueryString parameters, as follows:

C#
builder.QueryString["cat"] = 12345;
builder.QueryString["a"] = "A";
builder.QueryString["b"] = "B"; 

If the QueryString already contains a parameter contained in the URI passed to the constructor (in this case “cat”), the value of the parameter will be overwritten with the new value. If the parameter doesn’t already exist, it is appended to the QueryString generated. In addition, all the properties and methods of the internal StringDictionary object used to store the QueryString pairs are made available to the user. You could, for example, remove one of the parameters:

C#
builder.QueryString.Remove("cat"); 

Or check if the collection contains a given key or value:

C#
builder.QueryString.ContainsKey("cat");
builder.QueryString.ContainsValue("12345");

Lastly, there are two ways in which the URI may be consumed. Firstly, by calling:

C#
string uri = builder.ToString(); 

Or simply by calling:

C#
builder.Navigate(); 

which will perform a redirect to the URI currently contained in the object.

Happy coding.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Qwertie28-Feb-14 8:49
Qwertie28-Feb-14 8:49 
SuggestionPlease add to NuGet Pin
Shimmy Weitzhandler1-Nov-12 10:54
Shimmy Weitzhandler1-Nov-12 10:54 
GeneralRe: Please add to NuGet Pin
Shimmy Weitzhandler1-Nov-12 11:59
Shimmy Weitzhandler1-Nov-12 11:59 
Generallicence info Pin
Shameet Doshi21-Jul-10 3:09
Shameet Doshi21-Jul-10 3:09 
GeneralRe: licence info Pin
lotuspro21-Jul-10 3:29
lotuspro21-Jul-10 3:29 
GeneralNull Exception Issue Pin
j105 Rob19-Dec-06 12:49
j105 Rob19-Dec-06 12:49 
GeneralYou're great! Thanks for your sharing! Pin
zhang.hong8-Dec-06 20:47
zhang.hong8-Dec-06 20:47 
GeneralAmazing helper! Pin
Raven#37710-Aug-06 11:40
Raven#37710-Aug-06 11:40 
AnswerRe: Amazing helper! Pin
lotuspro11-Aug-06 0:32
lotuspro11-Aug-06 0:32 
GeneralMinor problem Pin
Oskar Austegard18-Aug-05 8:13
Oskar Austegard18-Aug-05 8:13 
GeneralRe: Minor problem Pin
lotuspro18-Aug-05 9:49
lotuspro18-Aug-05 9:49 
GeneralSimilar to my article :-) Pin
Uwe Keim20-Jul-05 10:24
sitebuilderUwe Keim20-Jul-05 10:24 
GeneralRe: Similar to my article :-) Pin
Oskar Austegard18-Aug-05 7:34
Oskar Austegard18-Aug-05 7:34 
GeneralRe: Similar to my article :-) Pin
Uwe Keim18-Aug-05 7:57
sitebuilderUwe Keim18-Aug-05 7:57 
Wow, cool. I would be glad if you publish the results of your merge.

--
Affordable Windows-based CMS for only 99 €: try www.zeta-producer.com for free!


GeneralRe: Similar to my article :-) Pin
Oskar Austegard18-Aug-05 8:23
Oskar Austegard18-Aug-05 8:23 
GeneralRe: Similar to my article :-) Pin
lotuspro18-Aug-05 9:57
lotuspro18-Aug-05 9:57 
GeneralRe: Similar to my article :-) Pin
Oskar Austegard18-Aug-05 10:14
Oskar Austegard18-Aug-05 10:14 
GeneralRe: Similar to my article :-) Pin
lotuspro18-Aug-05 11:10
lotuspro18-Aug-05 11:10 
GeneralNice KISS job... one minor change. Pin
Marc Brooks20-Jul-05 8:34
Marc Brooks20-Jul-05 8:34 
GeneralRe: Nice KISS job... one minor change. Pin
lotuspro20-Jul-05 9:02
lotuspro20-Jul-05 9:02 
GeneralRe: Nice KISS job... one minor change. Pin
Thomas Freudenberg25-Jul-05 21:08
Thomas Freudenberg25-Jul-05 21:08 
GeneralRe: Nice KISS job... one minor change. Pin
lotuspro25-Jul-05 22:44
lotuspro25-Jul-05 22:44 
GeneralNice and clean Pin
Ashaman20-Jul-05 7:22
Ashaman20-Jul-05 7:22 
GeneralNOTE From Author - Broken download link Pin
lotuspro20-Jul-05 4:46
lotuspro20-Jul-05 4:46 
GeneralUrlBuilder_src.zip doesn't exist Pin
DeKale20-Jul-05 3:49
DeKale20-Jul-05 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.