|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionBuilding 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 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 The 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 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 builder.PageName = “06.aspx”;
So far, apart from the 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 builder.QueryString.Remove(“cat”);
Or check if the collection contains a given key or value: builder.QueryString.ContainsKey(“cat”);
builder.QueryString.ContainsValue(“12345”);
Lastly, there are two ways in which the URI may be consumed. Firstly, by calling: string uri = builder.ToString();
Or simply by calling: builder.Navigate();
which will perform a redirect to the URI currently contained in the object. Happy coding.
|
||||||||||||||||||||||