QueryString checking QueryString checking asp.net asp.net

QueryString checking


You can determine if there are any values in the QueryString by checking its count:

Request.QueryString.Count > 0;

That said if you are trying to prevent a page from erroring because you don't want to access a value that is not there I recommend wrapping query parms up in page properties and returning safe values from the property.

As an example

// setting this as protected makes it available in markupprotected string TaskName{    get { return (string)Request.QueryString["VarName"] ?? String.Empty; }}


Check for

Request.QueryString["QueryStringName"]

if you know the particular name and it returns null if there isn't any querystring by that name

or if you want to check the count of querystrings then

Request.QueryString.Count

and check against 0. If greater than 0 then there is atleast 1 string appended.


To check if the page was accessed with any query string, you can check the Count property:

bool expression = Request.QueryString.Count > 0;

To access a defined query string parameter, you can do it like this:

string myParam = Request.QueryString["MyParam"];

myParam will be null if it is not on the URL.