在ASP.NET页面的上下文中,我可以使用Request.QueryString来获取URI的查询字符串部分中的键/值对的集合。
例如,如果我使用http://local/Default.aspx?test=value
加载我的页面,那么我可以调用以下代码:
//http://local/Default.aspx?test=value
protected void Page_Load(object sender, EventArgs e)
{
string value = Request.QueryString["test"]; // == "value"
}
理想情况下,我想要做的是检查是否存在,这样我就可以使用调用页面,并获得一个布尔值,告诉我查询字符串中是否存在。如下所示:
//http://local/Default.aspx?test
protected void Page_Load(object sender, EventArgs e)
{
bool testExists = Request.QueryString.HasKey("test"); // == True
}
所以理想情况下,我想要的是一个布尔值,它告诉我测试变量是否出现在字符串中。
我想我可以使用正则表达式来检查字符串,但是我很好奇是否有人有更好的解决方案。
我尝试过以下几种方法:
//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")
此行为与PHP不同,例如,在PHP中,我只能使用
$testExists = isset($_REQUEST['test']); // == True
https://stackoverflow.com/questions/14902059
复制相似问题