Check if non-valued query string exists in url with C#
If you do not specify a value, the key will be automatically set to null, so you cannot check its existence.
In order to check if the value actually exists, you can check in the collection of Values equalling null
if it contains your Key:
Request.QueryString.GetValues(null).Contains("query")
It returns null
because in that query string it has no value for that key. I think the check you're looking for is this:
if(Request.QueryString.Keys.OfType<string>().Any(k => k == "query"))
or even:
if(Request.QueryString.AllKeys.Any(k => k == "query"))
The latter is probably more appropriate because that array is already cached.
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.