我有一个error.aspx
页面。如果用户访问该页面,则它将使用Request.QueryString["aspxerrorpath"]
获取page_load()
方法URL中的错误路径,它工作正常。如何检查Request.QueryString在ASP.NET中是否具有特定值?
但是,如果用户直接访问该页面,它将生成一个异常,因为aspxerrorpath
不存在。
我该如何检查aspxerrorpath
是否存在?
我有一个error.aspx
页面。如果用户访问该页面,则它将使用Request.QueryString["aspxerrorpath"]
获取page_load()
方法URL中的错误路径,它工作正常。如何检查Request.QueryString在ASP.NET中是否具有特定值?
但是,如果用户直接访问该页面,它将生成一个异常,因为aspxerrorpath
不存在。
我该如何检查aspxerrorpath
是否存在?
你可以只检查null
:
if(Request.QueryString["aspxerrorpath"]!=null)
{
//your code that depends on aspxerrorpath here
}
谢谢你的工作我以前试过,但那个时候它不工作,但现在它的工作..可能是错过了那个时间的东西。:-) – Peeyush 2011-12-30 10:33:21
@Peeyush你可能会发现你试图将它转换为'.ToString ()',因此在测试null值本身之前试图将null转换为字符串值。这是一个常见的错误。 – 2014-02-10 10:59:55
请注意,如果QueryString中没有键值,则无法检查该键是否存在。 – 2016-09-21 09:54:50
string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"]) //true -> there is no value
将返回如果有一个值
检查参数的值:
// .NET < 4.0
if (string.IsNullOrEmpty(Request.QueryString["aspxerrorpath"]))
{
// not there!
}
// .NET >= 4.0
if (string.IsNullOrWhiteSpace(Request.QueryString["aspxerrorpath"]))
{
// not there!
}
如果是这样不存在,值将是null
,如果它确实存在,但没有val你设置它将是一个空字符串。
我相信上述内容将满足您的需求,而不仅仅是针对null
的测试,因为空字符串对您的具体情况同样糟糕。
而如果.NET == 4.0呢?使用'IsNullOrWhiteSpace'?如果.NET == 4.0,则为 – Chris 2014-04-23 21:00:50
?使用IsNullOrWhiteSpace – 2018-01-08 18:43:34
要解决您的问题,请在页面的Page_Load方法中写入以下行。
if (String.IsNullOrEmpty(Request.QueryString["aspxerrorpath"])) return;
.NET 4.0提供了更仔细看看空,空或空白的字符串,用它作为显示在下面的一行:
if(string.IsNullOrWhiteSpace(Request.QueryString["aspxerrorpath"])) return;
这将不会运行你的下一个语句(您的业务逻辑)如果查询字符串没有aspxerrorpath。
你还可以尝试:
if (!Request.QueryString.AllKeys.Contains("aspxerrorpath"))
return;
要检查你应该使用Request.QueryString.HasKeys
财产空查询字符串。
要检查钥匙存在:Request.QueryString.AllKeys.Contains()
然后你就可以得到IST的价值和做任何其他的检查要,如isNullOrEmpty等
当键存在但没有值时,Request.QueryString.AllKeys.Contains()'实际上会返回false。 – 2016-09-21 09:59:34
什么更直接的方式?
if (Request.QueryString.AllKeys.Contains("mykey")
认为你正在寻找的检查是这样的:
if(Request.QueryString["query"] != null)
它返回null
因为这样的查询字符串有此键的值。
Request.QueryString.GetValues(null).Contains(“query”) – user5690126 2015-12-17 08:46:17
我也试过使用Request.QueryString.Count!= 0,但后来的问题是,如果用户附加任何东西像?abc = 1223然后再次它会给异常 – Peeyush 2011-12-30 10:27:13
'Request.QueryString.Count!= 0'只会告诉你是否有_no_参数。 – Oded 2011-12-30 10:33:00