Tonight was working on a few items for the NTeamProject[^] website and found myself dealing with querystrings in a manual fashion. Knowing that I could build out a querystring object to handle a lot of this grunt work for me, I tried to save myself a few minutes time by doing a quick google search to see if I could find existing code doing what I am trying to accomplish. After just a couple searches in Google, I stumbled upon this gem[^]. Fairly straight forward, and easy to implement. Even has a few features I don't see myself using, at least not yet, like AbsoluteUri, or VirtualFolder since it's already pretty easy to get this info from the .Net framework.
One addition I did make was to add a method that allowed you to add a NameValueCollection to the QueryString object. By default the only way you can add a name/value pair is to call the .Add(name,value) method. Here is the code for the new method I have added. It does a check for each key in the NameValueCollection you pass to it to make sure it does not already exist. If it does, it removes it, if it does not, it calls the Set method to add the key and its value.
1/// <summary> 2/// Use this method to add a NameValueCollection object to the parent object. 3/// Validation is done to ensure the Key does not already exist in the QueryString object. 4/// </summary> 5/// <param name="collection"></param> 6public void Set(NameValueCollection collection) 7{ 8 foreach(string key in collection.Keys) 9 { 10 if(this.QueryString[key] != null) 11 { 12 this.QueryString.Remove(key); 13 } 14 this.Set(key,collection[key]); 15 } 16} Hopefully someone finds this useful.
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5