In this article, we will learn how to check if the URL contains QueryString or not in c#. I need to check if this URI contains some parameters or not.
For example, we consider these URL as an example
http://www.website.com/page?ID=9 // must return True
http://www.website.com/page?ID=9&q=tutorialsrack // must return True
http://www.website.com/page // must return False
Example 1: We can use HasKeys
method of Request.QueryString
to check if the URL contains QueryString or not.
bool hasKeys = HttpContext.Current.Request.QueryString.HasKeys();
if (hasKeys) // if true
{
//if URI Contains Parameter
//your code
}
else {
//your code
}
Example 2: Another Example To Check whether URL Contains a QueryString or not.
if (HttpContext.Current.Request.QueryString != null && HttpContext.Current.Request.QueryString.Count > 0)
{
//if URI Contains Parameter
//Your Code
}
else
{
//Your Code
}
I hope this article will help you to understand how to check if the URL contains QueryString or not in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments