Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 875
Daniel Miller Avatar asked Dec 31 '13 15:12

Daniel Miller


People also ask

How do I know if a URL contains a query string?

To check if a url has query parameters, call the indexOf() method on the url, passing it a question mark, and check if the result is not equal to -1 , e.g. url. indexOf('? ') !== -1 .

How do I check if a query string parameter exists?

Check if a query string parameter existsThe URLSearchParams.has() method returns true if a parameter with a specified name exists.

What is request QueryString in C#?

A Query String collection is a parsed version of the QUERY_STRING variable in the Server Variables collection. It enable us to retrieve the QUERY_STRING variable by name. When we use parameters with Request. QueryString, the server parses the parameters sent to the request and returns the effective or specified data.


1 Answers

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")
like image 80
Ludovic Feltz Avatar answered Sep 28 '22 03:09

Ludovic Feltz