Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if unassigned variable exists in Request.QueryString

Within the context of an ASP.NET page, I can use Request.QueryString to get a collection of the key/value pairs in the query string portion of the URI.

For example, if I load my page using http://local/Default.aspx?test=value, then I can call the following code:

//http://local/Default.aspx?test=value

protected void Page_Load(object sender, EventArgs e)
{
    string value = Request.QueryString["test"]; // == "value"
}

Ideally what I want to do is check to see if test exists at all, so I can call the page using http://local/Default.aspx?test and get a boolean telling me whether test exists in the query string. Something like this:

//http://local/Default.aspx?test

protected void Page_Load(object sender, EventArgs e)
{
    bool testExists = Request.QueryString.HasKey("test"); // == True
}

So ideally what I want is a boolean value that tell me whether the test variable is present in the string or not.

I suppose I could just use regex to check the string, but I was curious if anybody had a more elegant solution.

I've tried the following:

//http://local/Default.aspx?test

Request.QueryString.AllKeys.Contains("test"); // == False  (Should be true)
Request.QueryString.Keys[0];                  // == null   (Should be "test")
Request.QueryString.GetKey(0);                // == null   (Should be "test")

This behavior is different than PHP, for example, where I can just use

$testExists = isset($_REQUEST['test']); // == True
like image 763
Aaron Blenkush Avatar asked Feb 15 '13 19:02

Aaron Blenkush


People also ask

What is request QueryString ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter). Count.

How do I check if a query string parameter exists?

includes method allows us to check if a substring is contained in a string. If the string contains a question mark, then it has query parameters. The includes method returns a boolean result: true if the substring is contained in the string.

How do you check if a query string is null or not?

Request. QueryString. Count != 0 will simply tell you if there are no parameters at all.

How check query string is null or not in C#?

The String class in the System namespace provides the IsNullOrEmpty() method to check if a string is null or an empty string(""). This is a handy method to validate user input. IsNullOrEmpty() takes a string as an input and returns a Boolean value that depends on whether or not the string is null or empty.


4 Answers

Request.QueryString.GetValues(null) will get a list of keys with no values

Request.QueryString.GetValues(null).Contains("test") will return true

like image 171
Joe Avatar answered Oct 13 '22 20:10

Joe


I wrote an extension method to solve this task:

public static bool ContainsKey(this NameValueCollection collection, string key)
{
    if (collection.AllKeys.Contains(key)) 
        return true;

     // ReSharper disable once AssignNullToNotNullAttribute
    var keysWithoutValues = collection.GetValues(null);
    return keysWithoutValues != null && keysWithoutValues.Contains(key);
}
like image 44
Dark Daskin Avatar answered Oct 13 '22 22:10

Dark Daskin


Request.QueryString is a NameValueCollection, but items are only added to it if the query string is in the usual [name=value]* format. If not, it is empty.

If your QueryString was of the form ?test=value, then Request.QueryString.AllKeys.Contains("test") would do what you want. Otherwise, you're stuck doing string operations on Request.Url.Query.

like image 39
Oren Melzer Avatar answered Oct 13 '22 22:10

Oren Melzer


I use this.

if (Request.Params["test"] != null)
{
    //Is Set
}
else if(Request.QueryString.GetValues(null) != null && 
       Array.IndexOf(Request.QueryString.GetValues(null),"test") > -1)
{
    //Not set
}
else
{
    //Does not exist
}
like image 41
Dtagg Avatar answered Oct 13 '22 22:10

Dtagg