Check if an unassigned variable exists in Request.QueryString

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

For example, if I load my page using http://local/Default.aspx?test=value , 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, I want to check if a test exists at all, so I can call the page using http://local/Default.aspx?test and get a logical message about whether the test exists in the query string. Something like that:

 //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 that tells me if there is a test variable in a string or not.

I suppose I could just use a regular expression to validate a string, but I was curious if anyone had a more elegant solution.

I 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 from PHP, for example, where I can just use

 $testExists = isset($_REQUEST['test']); // == True 
+17
Feb 15 '13 at 19:36
source share
5 answers

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

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

+25
Feb 15 '13 at 19:50
source share

I wrote an extension method to solve this problem:

 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); } 
+5
Oct 10 '13 at 11:21
source share

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

If your QueryString has the form ?test=value , then Request.QueryString.AllKeys.Contains("test") will do what you want. Otherwise, you perform string operations on Request.Url.Query .

+2
Feb 15 '13 at 19:46
source share

I am using 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 } 
+2
Jan 14 '16 at
source share
 Request.QueryString.ToString().Contains("test") 

This works in the special case when you are looking for a single querystring parameter, for example. MyFile.aspx?test

For more complex, general cases, then other solutions would be better.

-one
Nov 01 '13 at 18:13
source share



All Articles