I am trying to set up a page that has two behaviors. I\'m separating them by URL: One behavior is accessed via /some-controller/some-action
, the other is via
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
).
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.
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
.
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.
You can test the value of Request.Url.Query
if ?customise
is the only think you're looking for.
While not directly part of this question, this additional information may also prove useful.
ASP.NET does not support query parameters seperated by ';'
which the W3C recommends that servers support as an alternative to '&'
.
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
.
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 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.
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 |
+--------------+--------------+
ASP.NET does not support determining the presence of query string parameters without values since Request.QueryString["customize"]
and Request.QueryString["foo"]
are both null
. You'll either have to parse it yourself or specify a value e.g. ?customize=1