vote up 0 vote down
star

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. Furthermore, this page states that this is a feature, not a bug. Go figure...

[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([&=]|$)")
offensive?
comments (3)

4 Answers:

vote up 0 vote down

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|offensive?
add comment
vote up 2 vote down

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

link|offensive?
add comment
vote up 0 vote down

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

link|offensive?
comments (1)
vote up 1 vote down

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|offensive?
comments (1)

Your Answer:

Get an OpenID
or

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