Access Query string parameters with no values in ASP.NET

Keyless Parameters

John Sherman’s answer is only technically correct. Query parameters without values are not supported, but query values without keys are supported.

In other words, "/some-controller/some-action?customize" is considered to be a URL with one query parameter whose value is "customize", and which has no key (i.e. a key of null).

Retrieving

To retrieve all such query parameters you use Request.QueryString.GetValues(null) to retrieve them as an array of strings, or you can use Request.QueryString[null] to get them as a single comma delimited string.

Empty Parameters

An empty parameter (as occurs in "?foo=bar&&spam=eggs"), will show up as a value of string.Empty with a key of null, as does a trailing "&".

The rather unusal query string "?&", for example, will show up as two values of string.Empty, both having a key of null.

The Empty Query String

There is one edge case which does not fit the pattern. That is the empy but present query string (e.g. “/some-controller/some-action?”). Based on the pattern previously shown it would have one value, namely string.Empty with a key of null. However, in reality it will have no values.

Leave a Comment