1

I am using a query string with 2 parameters name/DOB and id, in which first parameter name changes according to the necessity with DOB, i.e; either it could be name or DOB at one time, now I want to do something on the basis of parameter name, how can I achieve this.

For example this is the url www.example.com/detail.aspx?name=john&id=10 it could be www.example.com/detail.aspx?DOB=10/01/2009?id=10

pseudo code is something like this

if(first parameter name == "DOB")
{
   // do something
} 

please let me know if I can achieve this. Thanks in advance.

2
  • The order in which you add the querystring parameters is generally deemed irrelevant. Why do you specifically need this? Commented Jul 2, 2014 at 13:40
  • 1
    Note that the date format for your DOB parameter could get you into trouble, front slashes can be a problem, even when they are url encoded. Commented Jul 2, 2014 at 13:42

2 Answers 2

3

You can use Request.QueryString.AllKeys[0] to get the first query string key. See NameValueCollection.AllKeys on MSDN for more info.

By the way, it's probably a bad design to count on the query string being in any particular order. Instead, check for query string parameters like this...

if(!String.IsNullOrEmpty(Request.QueryString["DOB"])
{
//QS contains DB, now make sure it's a valid value
}
Sign up to request clarification or add additional context in comments.

Comments

0

Querystring has a collection of the keys, you can use the following code

if(Request.QueryString.AllKeys.FirstOrDefault() == "DOB")
{
   // do something
}

Alternately you can use Contains to see if the query string has the desired key, not necessarily first.

if(Request.QueryString.AllKeys.Contains("DOB"))
{
   // do something
}

1 Comment

Just a note, make sure you have using System.Linq at the top of your class, as this relies on an extension method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.