How do I send a URL with Query Strings as a Query String
Solution 1
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.
Solution 2
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.
Solution 3
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.
Dov Miller
Updated on July 09, 2022Comments
-
Dov Miller almost 2 years
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.
-
Dov Miller about 12 yearsThat looks like three query strings to the MyURL1. I want name and ID to be query strings of MyURL2.
-
coopejoh about 12 yearsThen access current QueryString before redirect and pass parameters to next QueryString (the ones that you're interested of).
-
coopejoh about 12 years@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.
-
Gabe over 8 yearsURL 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/…
-
BornToCode over 7 yearsThe
HttpServerUtility.UrlDecode
that you mention is accessible through theHttpContext.Current.Server.UrlDecode
or justServer.UrlDecode
in webforms. You could also use the staticHttpUtility.UrlDecode
. -
HappyGoLucky over 3 yearsAs 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