Anton  Zimm
Anton Zimm

Reputation: 420

query string parameter without value, only key

i have question about query string in asp.net:

standart query string with query string parameter is "www.mysity.url?key1=value1&key2=value2", but i need only check has query string key or not...yes, one of the correct decisions: www.mysite.url?reset=true, but this excess syntax for me.

in markup i use something like "<a href='UrlHelper.GetResetUrl()'>Reset</a>", this method return "www.mysity.url?reset", but in user side markup i have "Reset"

Upvotes: 1

Views: 4551

Answers (3)

nunespascal
nunespascal

Reputation: 17724

If you do not specify the name for a parameter it is taken as null.

Its value would be reset

So you would have to check it as follows:

if(Request.QueryString[null]=="reset")
{
    //Take some reset action
}

Upvotes: 6

mortb
mortb

Reputation: 9859

All code that handles querystring parameters should be case insensitive. Browsers (or parts of internet infrastructure?) may convert the case.

One way to check if reset parameter is present in querystring:

bool reset = Request.Url.Query.IndexOf("reset", StringComparison.CurrentCultureIgnoreCase) > -1;

Upvotes: 0

Aristos
Aristos

Reputation: 66641

a Quick and dirty solution is:

if(Request.Url.Query.Contains("?reset"))
{
    // ok we have a reset
}

Assuming that you have a standard reset call ask as: www.mysity.url?reset and the reset url not have other parameters. If you have you can simple check for the reset keyword.

This code HttpContext.Current.Request["reset"] is always return null, so the next best thing if you like to make it hard, is to manual analyze your keys after the url.

Upvotes: 1

Related Questions