Access Query string parameters with no values in ASP.NET

后端 未结 4 578
天涯浪人
天涯浪人 2020-12-01 12:03

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

相关标签:
4条回答
  • 2020-12-01 12:34

    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.

    0 讨论(0)
  • 2020-12-01 12:42

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

    0 讨论(0)
  • 2020-12-01 12:44

    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 |
    +--------------+--------------+    
    
    0 讨论(0)
  • 2020-12-01 12:48

    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

    0 讨论(0)
提交回复
热议问题