30

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 /some-controller/some-action?customize.

It doesn't look like the Request.QueryString object contains anything, though, when I visit the second URL...I mean, the keys collection has one element in it, but it's null, not 'customize'. Anyone have any ideas about this or how to enable this. I'd like to avoid manually parsing the query string at all costs :).

user2864740
  • 60,010
  • 15
  • 145
  • 220
Brad Heller
  • 1,551
  • 5
  • 18
  • 29

4 Answers4

73

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.

Kevin Cathcart
  • 9,838
  • 2
  • 36
  • 32
27

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 |
+--------------+--------------+    
Kevin Cathcart
  • 9,838
  • 2
  • 36
  • 32
  • key: `null` value: `edit` as a result of parsing `&edit&` looks very strange. I suppose it should be vice versa - key: `edit` value: `null`. – Bogdan Gusiev Sep 17 '15 at 12:06
  • 3
    You would think it would be the other way around, but that table is actually correct. God only knows hat microsoft was thinking when they coded it that way. We are all intuitively expecting a key with null value. Sigh. – Kevin Cathcart Sep 17 '15 at 14:42
7

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

Robin Day
  • 100,552
  • 23
  • 116
  • 167
  • 1
    This is the best solution, I think. I just did `Request.Url.Query.StartsWith()`. When dealing with semantic URLs it would be REALLY great to have support for this out of the box! – Brad Heller Jun 28 '10 at 06:57
  • @Toft: your method only works if you just have "customize" as the only parameter. I used: `bool hasCustomise = context.Request.Url.Query.Split(new char[] { '&', '?' }).Contains("customise");` (You will need to have using `System.Linq;` in your page.) – davidthegrey Mar 20 '18 at 11:38
1

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

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • 1
    Suddenly realizing I may be wrong and it might be possible to do `Request.QueryString.HasKey("customize")` but I remember going down this road in the past and not being able to find a solution – John Sheehan Jun 28 '10 at 06:53
  • 2
    After searching around, it looks like the QueryString dictionary values are only populated with parameters that have both a name AND a value so HasKey won't work. – John Sheehan Jun 28 '10 at 06:57
  • 2
    Indeed. What's particularly funny about it, though, is if you do supply a `?customize` query string, the QueryString dictionary will populate a single key, but it is null in the collection -- it doesn't have a related value, for whatever reason. Kind of strange behavior I think! – Brad Heller Jun 28 '10 at 07:07
  • 5
    what's even funnier is that if you do Request.QueryString.ToString(), it shows the key! – Jiho Han Jun 16 '11 at 17:29
  • 3
    http://stackoverflow.com/a/7519540/49942 shows a way to do it, which does work: what you thought were "value-less" parameters can be queried as "key-less" parameters. – ChrisW Oct 03 '13 at 14:11
  • 1
    I would gladly give up the accepted answer for the one below if I could. – John Sheehan Oct 03 '13 at 22:52