In the context of an ASP.NET page, I can use Request.QueryString to get a collection of key / value pairs in the query string part of the URI.
For example, if I load my page using http://local/Default.aspx?test=value , 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, I want to check if a test exists at all, so I can call the page using http://local/Default.aspx?test and get a logical message about whether the test exists in the query string. Something like that:
//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 that tells me if there is a test variable in a string or not.
I suppose I could just use a regular expression to validate a string, but I was curious if anyone had a more elegant solution.
I 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 from PHP, for example, where I can just use
$testExists = isset($_REQUEST['test']);
Aaron Blenkush Feb 15 '13 at 19:36 2013-02-15 19:36
source share