I wanted to remove the dependency on System.Web so that I could parse the query string of a ClickOnce deployment, while having the prerequisites limited to the "Client-only Framework Subset".
Here is a simple method that will accomplish this task. I have also included some unit tests. And, last but not least, I posted this on StackOverflow. Thanks to rp for writing the initial method.
Code
- public static NameValueCollection ParseQueryString(string s)
- {
- NameValueCollection nvc = new NameValueCollection();
- // remove anything other than query string from url
- if(s.Contains("?"))
- {
- s = s.Substring(s.IndexOf('?') + 1);
- }
- foreach (string vp in Regex.Split(s, "&"))
- {
- string[] singlePair = Regex.Split(vp, "=");
- if (singlePair.Length == 2)
- {
- nvc.Add(singlePair[0], singlePair[1]);
- }
- else
- {
- // only one key with no value specified in query string
- nvc.Add(singlePair[0], string.Empty);
- }
- }
- return nvc;
- \
Unit Tests
- [TestMethod]
- public void ParseQueryString_OneNameValueParameter()
- {
- //
- // TODO: Add test logic here
- //
- const string queryString = "http://www.foo.com/testpage.htm?mykey=myvalue";
- var result = Helper.ParseQueryString(queryString);
- Assert.AreEqual(1, result.Count);
- Assert.AreEqual("myvalue", result["mykey"]);
- }
- [TestMethod]
- public void ParseQueryString_ThreeNameValueParameter()
- {
- const string queryString = "http://www.foo.com/?mykey1=myval1&mykey2=myval2&mykey3=myval3";
- var result = Helper.ParseQueryString(queryString);
- Assert.AreEqual(3, result.Count);
- Assert.AreEqual("myval1",result["mykey1"]);
- Assert.AreEqual("myval2", result["mykey2"]);
- Assert.AreEqual("myval3", result["mykey3"]);
- }
- [TestMethod]
- public void ParseQueryString_OneKeyWithNoValue()
- {
- const string queryString = "http://www.foo.com/?mykey1";
- var result = Helper.ParseQueryString(queryString);
- Assert.AreEqual(1,result.Count);
- Assert.AreEqual(string.Empty, result["mykey1"]);
- }
- [TestMethod]
- public void ParseQueryString_MultipleKeysWithNoValue()
- {
- const string queryString = "http://www.foo.com/?mykey1=myval1&mykey2&mykey3=myval3";
- var result = Helper.ParseQueryString(queryString);
- Assert.AreEqual(3, result.Count);
- Assert.AreEqual("myval1", result["mykey1"]);
- Assert.AreEqual(string.Empty, result["mykey2"]);
- Assert.AreEqual("myval3", result["mykey3"]);
- }
- [TestMethod]
- public void ParseQueryString_NoUrlSpecifiedWithQueryString()
- {
- const string queryString = "mykey1=myval1&mykey2=myval2";
- var result = Helper.ParseQueryString(queryString);
- Assert.AreEqual(2, result.Count);
- Assert.AreEqual("myval1", result["mykey1"]);
- Assert.AreEqual("myval2", result["mykey2"]);
- \


2 comments:
Thanks, this was a great help. There is just on ething missing though. If the query string contains 'special characters, the routine does not decode that. The HttpUtility class does do that.
Thanks
Thanks! Great helper class (I'm using it for WP7 Navigation) and I'm glad to see the unit tests :)
Post a Comment