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.


Other Query Parameter Information

While not directly part of this question, this additional information may also prove useful.

The Seperator Character

ASP.NET does not support query parameters seperated by ';' which the W3C recommends that servers support as an alternative to '&'.

Parameters Starting with "="

Query parameters that start with '=', are considered to have a key, which would be string.Empty. Do not confuse this with a key of null. For example "/some-controller/some-action?=baz" has one value whose key is string.Empty and whose value is baz.

More Than One "=" Character

If there is more than one '=' character, the key is everything before the first '=', and the value is everythin after it.

For example "/some-controller/some-action?foo=bar=baz" has one paramter with a key of "foo" and a value of bar=baz.

Annother example "/some-controller/some-action?eggs==spam" has one parameter with a key of "eggs", and a value of "=spam".

Multiple Parameters of the Same Name

Multiple parameters of the same name are also supported, as hinted at in my other answer.

For example if the URL is "/some-controller/some-action?foo=bar&foo=baz", then the result of Request.QueryString["foo"] is `"bar,baz".

If you want each string seperately, use Response.QueryString.GetValues("foo"), which returns an array of strings.

Example

The The following highly implasuble URL would be considered to have six parameters:
"/some-controller/some-action?=baz&foo=bar&edit&spam=eggs=ham&==&"

They are:

+--------------+--------------+
|     Key      |     Value    |
+--------------+--------------+
| string.Empty | "baz"        |
| "foo"        | "bar"        |
| null         | "edit"       |
| "spam"       | "eggs=ham"   |
| string.Empty | "="          |
| null         | string.Empty |
+--------------+--------------+    

You can test the value of Request.Url.Query if ?customise is the only think you're looking for.