Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a URL with Query Strings as a Query String

Tags:

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.

like image 905
Dov Miller Avatar asked May 03 '12 12:05

Dov Miller


People also ask

How do I pass a URL in a query string?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

How do you give a URL a query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

Can I pass URL as query parameter?

Using &url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

What is a query string in a URL?

A query string is the portion of a URL where data is passed to a web application and/or back-end database. The reason we need query strings is that the HTTP protocol is stateless by design. For a website to be anything more than a brochure, you need to maintain state (store data).


2 Answers

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.

like image 188
Remko Jansen Avatar answered Oct 09 '22 13:10

Remko Jansen


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.

like image 45
JulioHM Avatar answered Oct 09 '22 13:10

JulioHM