Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the request URI have got any parameters

I have my class on C# on .NET, called after a request by an URI. I need to check if this URI contains some parameters or not.

For example :

http://www.website.com/page.aspx?ID=9           // must return YES
http://www.website.com/page.aspx?ID=9&Page=2    // must return YES
http://www.website.com/page.aspx                // must return NO

is it possible? Of course I couldn't know the name for each parameters in the URI, so for a random querystring like :

http://www.website.com/page.aspx?RandomParameter=1

I need to return YES. Can I do it?

like image 468
markzzz Avatar asked Jul 27 '11 13:07

markzzz


3 Answers

You have to allow for Request.QueryString being null as well (no parameters).

to return a string:

return Request.QueryString != null && Request.QueryString.Count > 0 ? "YES" : "NO";

to return a bool:

return Request.QueryString != null && Request.QueryString.Count > 0;
like image 140
Chris Snowden Avatar answered Oct 04 '22 23:10

Chris Snowden


It has been quite a while since I have worked with these, but I believe something like the following should fit your needs:

Solution:

if(Request.QueryString != null && Request.QueryString.Count > 0)
{
     return "YES";
}
else
{
     return "NO";
}

Inline Solution (If this was all you needed to do):

return (Request.QueryString != null && Request.QueryString.Count > 0) ? "YES":"NO";

You can find more information on Request.QueryString here.

like image 22
Rion Williams Avatar answered Oct 04 '22 21:10

Rion Williams


Try this method, If the requested page has any querystirng parameters defined then it will return true, otherwise it will return false

if (Request.QueryString.HasKeys())
{
    Response.Write("The requested page URI has parameters");
}
like image 21
Waqar Janjua Avatar answered Oct 04 '22 21:10

Waqar Janjua