0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

it says Key: to[0] and Key: to[1] etc., I know I can format and check for "to["+%d+"]". But is there a way I can get 'to' as an array
Ok, I see. I updated the answer to show how to account for multiple values per key. You need the GetValues function
No.. I mean How do I know what values is the array id. It could be any other array in the query string, right ?
you can cycle through the ids as follows: foreach (string key in Request.QueryString.AllKeys) { Response.Write(key); }

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.