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&param2=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.

  • JasonL

    I’d be interested in seeing a link to your function … we recently ran into the same problem.

  • DuncanS

    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.

  • Thanks for the tip Duncan!

    I’ve reposted this information as a post so that everyone will benefit from it.

  • Pingback: Request.QueryString Wrongful Parsing - My Solution » Advanced .NET Debugging()

  • pmacouzet

    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.

  • 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) {
    IDictionary
    queryStringDictionary = new SortedDictionary(StringComparer.CurrentCultureIgnoreCase);

    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;
    }