简体   繁体   English

如何检查 QueryString 是否没有参数

[英]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.当我没有任何参数时,我在 Querystring 中遇到一个问题。请查找以下示例。 I have a below URL我有以下网址

1 Scenario 1 场景

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

and I'm checking one condition and it is throwing error Request is not available我正在检查一个条件,它抛出错误 Request is not available

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

2 Scenario 2 场景

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.谢谢大家,每个人的回答都是正确的问题是因为我的上下文。Qerystring 没有返回。所以,我在我的 aspx 页面中声明,它对我来说工作正常。

ASPX Code ASPX代码

 <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. Request.QueryString只不过是一个NameValueCollection ,即集合之一。 So like other collections it also has Count property.所以像其他集合一样,它也有Count属性。 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 页面中声明,它对我来说工作正常。

ASPX markup: ASPX 标记:

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

Code-behind:代码隐藏:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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