Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

BEWARE: System.Web.HttpValueCollection Parsing Is Not Reversible

If you run this code:

System.Collections.Specialized.NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString("noKey&=emptyKey&A=Akey");

queryString will actually have the running type of System.Web.HttpValueCollection.

What's great about this class is that its ToString method output is the collection's content in a nice URL encoded format.

As with its base class (NameValueCollection), there’s a difference between a null string and an empty string key and the parsing treats query string parameters with no parameter specification as having a null string key and parameters with an empty string key having an empty string parameter key.

So, when call ToString on the instance returned by System.Web.HttpUtility.ParseQueryString method you would expect to get the parsed string (or, at least, one that would be parsed into the equivalent collection), right? But what you’ll get instead is this: noKey&emptyKey&A=Akey.

I’ve filed a bug into connect. If you think this is important and must be corrected, please vote.

Comments

RichardD said:

The null key in the query-string collection can also have some pretty nasty side-effects:

connect.microsoft.com/.../ViewFeedback.aspx

If you use a DataPager control with a QueryStringField, and the query-string contains a null key, the page will die horribly with a NullReferenceException from the internal GetQueryStringNavigateUrl method.

# July 22, 2008 4:28 PM

Recent URLs tagged Querystring - Urlrecorder said:

Pingback from  Recent URLs tagged Querystring - Urlrecorder

# September 7, 2008 11:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)