检查未赋值的变量存在的Request.QueryString [英] Check if unassigned variable exists in Request.QueryString
问题描述
在ASP.NET页面的情况下,我可以使用的Request.QueryString取得URI的查询字符串部分中的键/值对的集合。
例如,如果我使用 HTTP负载我的网页://local/Default.aspx试验=价值
,那么我可以致电以下code:
//http://local/Default.aspx?test=value
保护无效的Page_Load(对象发件人,EventArgs的)
{
字符串值=的Request.QueryString [测试]; // ==值
}
理想的情况是我想要做的是检查,看看是否的测试的存在的话,这样我就可以打电话用的页面的http://本地/默认。 ASPX?测试
并得到一个布尔值的查询字符串是否存在测试告诉我。事情是这样的:
//http://local/Default.aspx?test
保护无效的Page_Load(对象发件人,EventArgs的)
{
布尔testExists = Request.QueryString.HasKey(测试); // ==真
}
因此,最好是我想要的是一个布尔值,它告诉我的测试变量是否为present字符串或没有。
我想我可以只使用正则表达式来检查字符串,但我很好奇,如果任何人有一个更好的解决方案。
我已经试过如下:
//http://local/Default.aspx?test
Request.QueryString.AllKeys.Contains(测试); // ==假(应该是真实的)
Request.QueryString.Keys [0]; // == NULL(应该是测试)
Request.QueryString.GetKey(0); // == NULL(应该是测试)
此行为是比PHP不同的,例如,在那里我可以只使用
$ testExists =使用isset($ _ REQUEST ['测试']); // ==真
Request.QueryString.GetValues(空)
将获得钥匙,没有值的列表。
Request.QueryString.GetValues(空)。载(测试)
将返回true
Within the context of an ASP.NET page, I can use Request.QueryString to get a collection of the key/value pairs in the query string portion of the URI.
For example, if I load my page using http://local/Default.aspx?test=value
, then I can call the following code:
//http://local/Default.aspx?test=value
protected void Page_Load(object sender, EventArgs e)
{
string value = Request.QueryString["test"]; // == "value"
}
Ideally what I want to do is check to see if test exists at all, so I can call the page using http://local/Default.aspx?test
and get a boolean telling me whether test exists in the query string. Something like this:
//http://local/Default.aspx?test
protected void Page_Load(object sender, EventArgs e)
{
bool testExists = Request.QueryString.HasKey("test"); // == True
}
So ideally what I want is a boolean value that tell me whether the test variable is present in the string or not.
I suppose I could just use regex to check the string, but I was curious if anybody had a more elegant solution.
I've tried the following:
//http://local/Default.aspx?test
Request.QueryString.AllKeys.Contains("test"); // == False (Should be true)
Request.QueryString.Keys[0]; // == null (Should be "test")
Request.QueryString.GetKey(0); // == null (Should be "test")
This behavior is different than PHP, for example, where I can just use
$testExists = isset($_REQUEST['test']); // == True
Request.QueryString.GetValues(null)
will get a list of keys with no values
Request.QueryString.GetValues(null).Contains("test")
will return true
这篇关于检查未赋值的变量存在的Request.QueryString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!