Skip to main content
1 of 8

I figure HttpUtility.ParseQueryString is just incredibly robust, and you likely are missing something. But I can't spot anything in particular and it's likely that without deep research you won't be able to cover all the cases which were clearly studied and thought out in Microsoft's implementation. However, I think your implementation can be refactored to be easier to read and maintain by using regular expressions instead brute force string splitting. I present the following in F# which hopefully you can translate easily enough to C#. I'm still new to regex writing myself, so I'm sure it's not as robust as it could be, but it seems to work well for all the typical cases I tested (assumes query string is not malformed, allows no query string, allows empty values):

open System
open System.Text.RegularExpressions
let uriParams uri =
    let matches = Regex.Matches(uri, @"[\?&](([^&=]+)=([^&=]*))", RegexOptions.Compiled)
    seq { //create a sequence of key, value tuples extracted from the matches
        for m in matches do 
            yield Uri.UnescapeDataString(m.Groups.[2].Value), Uri.UnescapeDataString(m.Groups.[3].Value)
    } |> dict // convert the sequence of key, value tuples into an immutable IDictionary