Sometimes its the little bugs that really get to you. They are so small that you spend quite a lot of time figuring out what’s wrong and then it hits you.
In my case, the problem was simple. I had a parameter being passed to me on the URL.
The URL looked something like this:
http://www.myapp.com/somepage.aspx?param1=text1%2c+text2%26text3¶m2=true
param1 has a URL encoded value which translates into (without the double quotes):
“text1, text2&text3“
Yeap, it has a “&” character as the value, but its URL encoded so it suppose to be OK.
When I went to get the value of param1 by using:
var string a = Request.QueryString["param1"];
The value I got was:
text1, text2
No text3 for some reason.
It seems that in certain cases there is a bug. I suspect that it URL decodes the query string before it cuts it up and fill in the Request.QueryString collection.
I didn’t fill like going through all the massive amounts of code of HttpRequest and friends using Reflector, so I just wrote a function that handles the URL decode and splitting issues correctly.
If you wish, I can post it as a link here.








Entries (RSS)
December 18th, 2006 at 12:47 am
I’d be interested in seeing a link to your function … we recently ran into the same problem.
January 4th, 2007 at 4:06 pm
I’ve run into this before but when dealing System.Uri. If you use Uri.ToString() it unescapes the QueryString – you should use Uri.AbsoluteUri.
January 4th, 2007 at 4:44 pm
Thanks for the tip Duncan!
I’ve reposted this information as a post so that everyone will benefit from it.
January 11th, 2007 at 7:41 pm
[...] Contact « Request.QueryString Wrongful Parsing Difference between performance data reported by different tools » 18 12 2006 [...]
October 3rd, 2007 at 7:52 pm
I am very interested on the function you prepared for the URL decode. If possible please post it.
Thanks in advance for all your help
pablo.
October 5th, 2007 at 4:50 pm
Hi Pablo,
Here is the function I used. It can probably be further optimized and it was written a while back… anyhow, here it goes:
public static IDictionary GetQueryString(string query) { queryStringDictionary = new SortedDictionary (StringComparer.CurrentCultureIgnoreCase);
IDictionary
if (!string.IsNullOrEmpty(query)) {
if (query[0] == ‘?’) {
query = query.Remove(0, 1);
}
string[] queryParts = query.Split(‘&’);
if (queryParts != null) {
string[] parts;
foreach (string queryPart in queryParts) {
parts = queryPart.Split(‘=’);
if (parts.Length > 0) {
if (parts.Length == 1) {
queryStringDictionary[parts[0]] = null;
}
else if (parts.Length == 2) {
queryStringDictionary[parts[0]] = HttpUtility.UrlDecode(parts[1]);
}
}
}
}
}
return queryStringDictionary;
}