Check QueryString

How to check if a webpage contains any string requests when loading the page?

+6
source share
4 answers

You can determine if there are any values ​​in a QueryString by checking its quantity:

Request.QueryString.Count > 0; 

This means that if you are trying to prevent a page error because you do not want to access a value that is not there, I recommend wrapping request packets in the page properties and returning safe values ​​from the property.

As an example

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

Check

 Request.QueryString["QueryStringName"] 

if you know a specific name and it returns null, if there is no such name using this name

or if you want to check the number of requests, then

 Request.QueryString.Count 

and check for 0. If greater than 0, then at least 1 row is added.

+18
source share

To check if a page has access to any query string, you can check the Count property:

 bool expression = Request.QueryString.Count > 0; 

To access a specific query string parameter, you can do this as follows:

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

myParam will be null if not specified in the URL.

+1
source share
 if(Request.QueryString.Count > 0) { //Code here } else { //Code here } 
0
source share

All Articles