up vote 1 down vote favorite
1

The Problem

What is the proper way to check for the foo parameter in the following url's querystring using asp.net? Is this even possible?

http://example.com?bar=3&foo

I have tried checking Request["foo"] as well as Request.QueryString["foo"] and I get null for both. I have also tried populating a List with the values from the QueryString collection but as I mention below, it does not include the value.

The Question

I understand that there is no value, but shouldn't Request["foo"] return an empty string rather than null? Is there a way to find out if a querystring key exists even if it has no value?

Notes

I found here that Request.QueryString.AllKeys includes null for blank querystring parameters.

[edit]

As stated below by James and Dreas a Regex to parse the raw url might be the best (and possibly only) approach.

Regex.IsMatch(Request.RawUrl, "[?&]thumb([&=]|$)")
link|flag

75% accept rate
What's the point of checking if a query string exists but is empty? It's value (lack of value actually) is just as useless. – Andreas Grech Dec 30 '08 at 14:29
1  
@Dreas, I assume that there is an implied "=true" at work (while the complete absence of a foo parameter is an implied "foo=false") – James Curran Dec 30 '08 at 14:32
bool doesFooExist = Request.Url.AbsoluteUri.IndexOf("foo=") >= 0 ? true : false; //will return true even if it doesnt have a value – Andreas Grech Dec 30 '08 at 14:34

5 Answers

up vote 4 down vote accepted

You can use null as the key for the NameValueCollection and it will give you a comma-delimited list of parameter names that don't have values.

For http://example.com?bar=3&foo you would use Request.QueryString[null] and it would retrieve foo.

If you have more than one parameter name without value, it will give you a value that is comma-delimited.

For http://example.com?bar=3&foo&test you would get foo,test as a value back.

link|flag
1  
You can actually use Request.QueryString.GetValues(null) to get the parameter names that don't have values. – Sysmo Aug 29 at 22:04
Perfect! This is exactly what I was looking for. Great Answer. – brad Aug 30 at 2:56
Better really late than never I guess... :-) – Sysmo Sep 7 at 14:28
up vote 3 down vote

Request.ServerVariables["QUERY_STRING"] will return the query string, complete, as a string. Then search it using a Regex or IndexOf

link|flag
up vote 2 down vote

You get null because the foo parameter doesn't have a value with it.

...What's the problem exactly?

If you still want to check for its existence (although it lacks a value), try something like this:

bool doesFooExist = Request.Url.AbsoluteUri.IndexOf("foo=") >= 0 ? true : false;
link|flag
I would like to check for a querystring key value even if it has no value. I've updated the question accordingly. – brad Dec 30 '08 at 14:26
up vote 0 down vote

Dreas is correct. Variable "bar" has a value but foo does not.

link|flag
Right, there is no value for foo. However, I can find no reference to foo other than in the raw url. A blank parameter is a parameter nonetheless. – brad Dec 30 '08 at 14:27
up vote 0 down vote

QueryString["Bar"] will return 3 because it has the value 3 associated with the variable Bar. However, Foo will return null because their is no value and when you are calling QueryString on a variable or key, you are querying for the value, not the key, so that is why you are returning null.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.