[英]Check if unassigned variable exists in Request.QueryString
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.在 ASP.NET 页面的上下文中,我可以使用 Request.QueryString 来获取 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
加载我的页面,那么我可以调用以下代码:
//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.理想情况下,我想要做的是检查测试是否存在,所以我可以使用http://local/Default.aspx?test
调用页面并获得一个布尔值,告诉我查询字符串中是否存在测试。 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这种行为与 PHP 不同,例如,我可以使用
$testExists = isset($_REQUEST['test']); // == True
Request.QueryString.GetValues(null)
will get a list of keys with no values Request.QueryString.GetValues(null)
将获取没有值的键列表
Request.QueryString.GetValues(null).Contains("test")
will return true Request.QueryString.GetValues(null).Contains("test")
将返回 true
I wrote an extension method to solve this task:我写了一个扩展方法来解决这个任务:
public static bool ContainsKey(this NameValueCollection collection, string key)
{
if (collection.AllKeys.Contains(key))
return true;
// ReSharper disable once AssignNullToNotNullAttribute
var keysWithoutValues = collection.GetValues(null);
return keysWithoutValues != null && keysWithoutValues.Contains(key);
}
Request.QueryString
is a NameValueCollection
, but items are only added to it if the query string is in the usual [name=value]*
format. Request.QueryString
是一个NameValueCollection
,但只有当查询字符串采用通常的[name=value]*
格式时才会向其中添加项目。 If not, it is empty.如果没有,则为空。
If your QueryString
was of the form ?test=value
, then Request.QueryString.AllKeys.Contains("test")
would do what you want.如果您的QueryString
是?test=value
形式,那么Request.QueryString.AllKeys.Contains("test")
会做您想做的事。 Otherwise, you're stuck doing string operations on Request.Url.Query
.否则,您将无法在Request.Url.Query
上执行字符串操作。
I use this.我用这个。
if (Request.Params["test"] != null)
{
//Is Set
}
else if(Request.QueryString.GetValues(null) != null &&
Array.IndexOf(Request.QueryString.GetValues(null),"test") > -1)
{
//Not set
}
else
{
//Does not exist
}
TRY this one, it solved my issue!试试这个,它解决了我的问题! It will count either the query string has a value or empty and then you can check the required query string value with the Key.它将计算查询字符串是否有值或为空,然后您可以使用键检查所需的查询字符串值。
if (!Page.IsPostBack)
{
if (Request.QueryString.Count > 0)
{
if (Request.QueryString["departmentId"] != null)
{
GetSurveyByDeptAndStatus(departmentId: Request.QueryString["departmentId"], status: "Not Surveyed");
rbListEmpsSurvey.Items[1].Selected = true;
}
else if (Request.QueryString["SurveyStatus"] != null)
{
SatisfactionStatus = Request.QueryString["SurveyStatus"] "";
GetSurveyByDeptAndStatus(status: SatisfactionStatus);
GetDepartments();
}}}
Request.QueryString.ToString().Contains("test")
This works in the special case where you're looking for a single querystring parameter, eg MyFile.aspx?test
这适用于您正在寻找单个查询字符串参数的特殊情况,例如MyFile.aspx?test
For more complex, general, cases then other solutions would be better.对于更复杂的一般情况,其他解决方案会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.