ILuvProgramming
ILuvProgramming

Reputation: 131

get a array of values from the url in asp.net

I am using ASP.NET for the first time.

Here is my issue. I have a ASP.NET page. some other website redirects to my webpage and the url is

http://localhost/mysite/newpage.aspx?request=424718&to%5B0%5D=111832&to%5B1%5D=1186313&to%5B2%5D=100009#_=_

actually what that website send is a request value and an array of id 's

I have to get those values in my page and display them.

I have used @Request["request"] to get the request value. But I do not know how to get the id values (since it is an array). I tried @Request["to"] and it gives null.

I also cannot understand that url encoding.. it is supposed to be like this

?request=3435&to=3495&to=435&to=3546

Upvotes: 0

Views: 3582

Answers (1)

Brian Webster
Brian Webster

Reputation: 30865

The ids & values that you are looking for, I believe, are in the URL. This is called the querystring.

Single Values

This code will output each key-value-pair from the querystring. If there are multiple values, they will be comma-delimited.

C#

foreach (String key in Request.QueryString.AllKeys)
{
    Response.Write("Key: " + key + " Value: " + Request.QueryString[key]);
}

VB.NET

For Each key as String in Request.QueryString.Allkeys
    Response.Write("Key: " & key & " Value: " & Request.QueryString(key))
Next

If you use the above code, you will notice that there is a comma-delimited list of values outputted for the to key. This is because the to key is used multiple times.

Multiple Values

This will output each key followed by each key's values.

foreach (String key in Request.QueryString.AllKeys)
{
    var values = Request.QueryString.GetValues(key);

    foreach (String item in values)
    {
      Response.Write("value: " + item + " ";
    }
}

This will output each key and then each value for each key, even if there are multiple.

Reference

Upvotes: 2

Related Questions