24

I am doing a redirect from one page to another and another redirect from the second page to a third. I have imformation from the first page which is not used on the second page but must be transfered to the third page. Is it possible to send the URL of the third page with its Query Strings as a Query String to the second page. Here's an example:

Response.Redirect("MyURL1?redi=MyURL2?name=me&ID=123");

My problem is that the URL being sent as a Query String has two Query String variables, so how will the system know that what's after the & is the second variable of the second URL and not a second variable of the first URL? Thank you.

1
  • Have you considered using Session variables for that ? Commented May 3, 2012 at 12:12

4 Answers 4

26

You must encode the url that you pass as a parameter in your redirect URL. Like this:

MyURL = "MyURL1?redi=" + Server.UrlEncode("MyURL2?name=me&ID=123");

This will create a correct url without the double '?' and '&' characters:

MyURL1?redi=MyURL2%3fname%3dme%26ID%3d123

See MSDN: HttpServerUtility.UrlEncode Method

To extract your redirect url from this encoded url you must use HttpServerUtility.UrlDecode to turn it into a correct url again.

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

2 Comments

URL decoding/encoding is not well defined. Every OS/browser/server has it's own idiocracies. The implmentation changed between .Netv1.1 and .Netv2.0 so I would recommend following the advice from @JulioHM and using base64 for URLs that need to be in query paramaters. bytes.com/topic/asp-net/answers/…
The HttpServerUtility.UrlDecode that you mention is accessible through the HttpContext.Current.Server.UrlDecode or just Server.UrlDecode in webforms. You could also use the static HttpUtility.UrlDecode.
4

Your query string should look like this:

MyURL1?redi=MyURL2&name=me&ID=123

Check: http://en.wikipedia.org/wiki/Query_string

You should have one ? sign and all parameters joined with &. If parameter values contain special characters just UrlEncode them.

3 Comments

That looks like three query strings to the MyURL1. I want name and ID to be query strings of MyURL2.
Then access current QueryString before redirect and pass parameters to next QueryString (the ones that you're interested of).
@DovMiller I'm just saying how the QueryString should look like. It's up to you how you obtain the QueryString you want to pass to next address - it's just a string - so you can use substring, etc. or just access parameter values from QueryString dictionary. Maybe, I don't fully understand what you're trying to achieve.
4

I find it helpful to encode query string parameters in Base64 before sending. In some cases this helps, when you need to send all kinds of special characters. It doesn't make for good debug strings, but it will protect ANYTHING you are sending from getting mixed with any other parameters.

Just keep in mind, the other side who is parsing the query string will also need to parse the Base64 to access the original input.

1 Comment

As suggested, used base64 in C# - Convert.FromBase64String, Convert.ToBase64String, and System.Text.Encoding.ASCII.GetString. Also needed it in JavaScript and used btoa and atob
0
using System.IO;
using System.Net;

static void sendParam()
{

    // Initialise new WebClient object to send request
    var client = new WebClient();

    // Add the QueryString parameters as Name Value Collections
    // that need to go with the HTTP request, the data being sent
    client.QueryString.Add("id", "1");
    client.QueryString.Add("author", "Amin Malakoti Khah");
    client.QueryString.Add("tag", "Programming");

    // Prepare the URL to send the request to
    string url = "http://026sms.ir/getparam.aspx";

    // Send the request and read the response
    var stream = client.OpenRead(url);
    var reader = new StreamReader(stream);
    var response = reader.ReadToEnd().Trim();

    // Clean up the stream and HTTP connection
    stream.Close();
    reader.Close();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.