Reputation: 3059
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
Upvotes: 20
Views: 25263
Reputation: 53
Request.QueryString.GetValues(vbNullString).Contains("test")
Although @Joe's answer is the correct answer, it doesn't account for VB.net
programmers. The VB
issue with @Joe's [correct] answer is that it yields an error at the "GetValues(null)
" section. vbNullString
alleviates the issue.
Additional Note
ClientQueryString.Contains("test")
might solve your problem (it did for me). Please know, though, that this solution has its pitfalls.
Either of these will [probably] get the job done for you:
Request.QueryString.GetValues(vbNullString).Contains("test")
ClientQueryString.Contains("test")
I would've added this as a comment, but I don't have enough reputation points (43 out of 50)
Upvotes: 1
Reputation: 2285
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();
}}}
Upvotes: 1
Reputation: 78
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
}
Upvotes: 2
Reputation: 3000
Request.QueryString.ToString().Contains("test")
This works in the special case where you're looking for a single querystring parameter, e.g. MyFile.aspx?test
For more complex, general, cases then other solutions would be better.
Upvotes: -2
Reputation: 760
Request.QueryString
is a NameValueCollection
, but items are only added to it if the query string is in the usual [name=value]*
format. 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. Otherwise, you're stuck doing string operations on Request.Url.Query
.
Upvotes: 2
Reputation: 1474
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);
}
Upvotes: 5
Reputation: 5487
Request.QueryString.GetValues(null)
will get a list of keys with no values
Request.QueryString.GetValues(null).Contains("test")
will return true
Upvotes: 29