dferraro
dferraro

Reputation: 6438

get raw (non-decoded) querystring key/values

How do I get the raw (non-decoded) values that are in the querystring?

I found Request.Url.Query, which does successfully give me the raw querystring itself. However, it gives me a type 'string' and the entire QS contents: how do I get the key/value pairs?

I did find HttpUtility.ParseQueryString, however that forces me to do decoding. I need the raw name/value pairs.

Must I implement my own querystring parsing for this? Has anyone done one I can use that ignores encoding? I found the Mono implementation however this also forces encoding.

Thanks!

Edit: the reason why I'm looking for this is because someone (not me!) wrote some code to encrypt a value and place it in the querystring. However they forgot to encode it first. The decoding that Request.Querystring() is doing is making it impossible to decrypt around 50% of these because the encryption leaves things like '+' and '.'

Upvotes: 3

Views: 2032

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156728

Request.QueryString should give you a NameValueCollection with all the key/value pairs, already decoded.

Sorry, I misunderstood the question. You'll need to parse it yourself, but it shouldn't be that difficult:

Request.Url.Query.Split('&').Select(pair => pair.Split('='))
    .ToDictionary(a => a[0], a => a[1]);

This is, of course, assuming that the unencoded values in your URLs don't contain "&" or "=" values. If they do... well, that's kind of what URL encoding is there for, isn't it?

Upvotes: 9

Related Questions