Daniel Miller Daniel Miller - 1 month ago 5
C# Question

Check if non-valued query string exists in url with C#

I've seen a couple examples of how to check if a query string exists in a url with C#:

www.site.com/index?query=yes


if(Request.QueryString["query"]=="yes")


But how would I check a string without a parameter? I just need to see if it exists.

www.site.com/index?query


if(Request.QueryString["query"] != null) //why is this always null?


I know there's probably a simple answer and I'll feel dumb, but I haven't been able to find it yet. Thanks!

Answer

If query was included as a parameter, but no value was specified, then the value of query will be null but it will still exist in Request.QueryString.AllKeys.

If query was not included, it won't exist in Request.QueryString.AllKeys at all.