简体   繁体   中英

How to Check If QueryString has no parameter

I am facing one issue in Querystring when I don't have any parameters.Please find the below example. I have a below URL

1 Scenario

URL ---http://localhost/Employee/Emp/empmanagement.aspx

and I'm checking one condition and it is throwing error Request is not available

if(Request.QueryString.ToString().Contains("employeeData"))

2 Scenario

URL ---http://localhost/Employee/Emp/empmanagement.aspx?empData=employeeData

and it is working fine below

if(Request.QueryString.ToString().Contains("employeeData"))

Thanks Guys everyone's answer is correct the issue was because of my context.Qerystring was not returing.So,i declared in my aspx page and it is working fine for me.

ASPX Code

 <cw:QueryString runat="server" ID="_empValue"  Required="False" />

Code Behind Code

if(_empValue.Value != null && _empValue.Value.Contains("employeeData")

This should be enough

if(Request != null && Request.QueryString["employeeData"] != null)
{
}

OR

if (Request != null && Request.QueryString.Keys.Count > 0)
{
}

OR

if (Request != null && string.IsNullOrEmpty(Request.QueryString["employeeData"]))
{

}

Request.QueryString is nothing but a NameValueCollection ie one of collection. So like other collections it also has Count property. So you can check

Request.QueryString.Keys.Count > 0

You can try

if (Request.QueryString.Keys.Count > 0)
{

}

or you can try

if(Request.QueryString.AllKeys.Any(i => i == "query"))

Try this:

if(Request!=null && Request.QueryString.Keys.Count > 0)
{
     if(Request.QueryString.ToString().Contains("employeeData"))
     {
     }
}

Thanks Guys, everyone's answer is correct the issue was because of my context query string was not returning sometimes.

So, I declared in my aspx page and it is working fine for me.

ASPX markup:

<cw:QueryString runat="server" ID="_empValue"  Required="False" />

Code-behind:

if(_empValue.Value != null && _empValue.Value.Contains("employeeData")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM