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

C#asp.net.Net

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

You can just check for null:

if(Request.QueryString["aspxerrorpath"]!=null)
{
   //your code that depends on aspxerrorpath here
}

Solution 2 - C#

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.

Solution 3 - C#

To check for an empty QueryString you should use Request.QueryString.HasKeys property.

To check if the key is present: Request.QueryString.AllKeys.Contains()

Then you can get ist's Value and do any other check you want, such as isNullOrEmpty, etc.

Solution 4 - C#

You can also try:

if (!Request.QueryString.AllKeys.Contains("aspxerrorpath"))
   return;

Solution 5 - C#

string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"]) //true -> there is no value

Will return if there is a value

Solution 6 - C#

What about a more direct approach?

if (Request.QueryString.AllKeys.Contains("mykey")

Solution 7 - C#

To resolve your problem, write the following line on your page's Page_Load method.

if (String.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) return;

.Net 4.0 provides more closer look to null, empty or whitespace strings, use it as shown in the following line:

if(string.IsNullOrWhiteSpace(Request.QueryString["aspxerrorpath"])) return;

This will not run your next statements (your business logics) if query string does not have aspxerrorpath.

Solution 8 - C#

think the check you're looking for is this:

if(Request.QueryString["query"] != null) 

It returns null because in that query string it has no value for that key.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeeyushView Question on Stackoverflow
Solution 1 - C#BrokenGlassView Answer on Stackoverflow
Solution 2 - C#OdedView Answer on Stackoverflow
Solution 3 - C#MarianoCView Answer on Stackoverflow
Solution 4 - C#shapiro yaacovView Answer on Stackoverflow
Solution 5 - C#PeterView Answer on Stackoverflow
Solution 6 - C#ProfKView Answer on Stackoverflow
Solution 7 - C#Imran RizviView Answer on Stackoverflow
Solution 8 - C#user5690126View Answer on Stackoverflow