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([&=]|$)")
You can use
nullas the key for theNameValueCollectionand it will give you a comma-delimited list of parameter names that don’t have values.For
http://example.com?bar=3&fooyou would useRequest.QueryString[null]and it would retrievefoo.If you have more than one parameter name without a value, it will give you a value that is comma-delimited.
For
http://example.com?bar=3&foo&testyou would getfoo,testas a value back.Update:
You can actually use
Request.QueryString.GetValues(null)to get the parameter names that don’t have values.