Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that Request.QueryString has a specific value or not in ASP.NET?

Tags:

c#

.net

asp.net

I have an error.aspx page. If a user comes to that page then it will fetch the error path in page_load() method URL using Request.QueryString["aspxerrorpath"] and it works fine.

But if a user directly accesses that page the it will generate an exception because aspxerrorpath is not there.

How can I check that aspxerrorpath is there or not?

like image 348
Peeyush Avatar asked Dec 30 '11 10:12

Peeyush


People also ask

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 ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).

Can we use QueryString with post method?

A POST request can include a query string, however normally it doesn't - a standard HTML form with a POST action will not normally include a query string for example.


2 Answers

You can just check for null:

if(Request.QueryString["aspxerrorpath"]!=null) {    //your code that depends on aspxerrorpath here } 
like image 138
BrokenGlass Avatar answered Oct 04 '22 19:10

BrokenGlass


Check for the value of the parameter:

// .NET < 4.0 if (string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) {  // not there! }  // .NET >= 4.0 if (string.IsNullOrWhiteSpace(Request.QueryString["aspxerrorpath"])) {  // not there! } 

If it does not exist, the value will be null, if it does exist, but has no value set it will be an empty string.

I believe the above will suit your needs better than just a test for null, as an empty string is just as bad for your specific situation.

like image 44
Oded Avatar answered Oct 04 '22 20:10

Oded