How to get value of QueryString inside QueryString

R

Ray Booysen

Mehdi said:
Hi,

I get the following URL on page load:

http://www.server1.com?RedirectUrl=http://www.server2.com?id=777

So QueryString["RedirectUrl"] will return (after cleaning up via URL
Encode/decode) :
"http://www.server2.com?id=777"

However I am interested to get the value of "777" without using string
functions.

Any suggestions?



Kind Regards


Mehdi

Use substring to retrieve all the chars after the ?. You can find the
starting position of ? using the String static methods too.
 
M

Mehdi

You left off "&" delimiter...

& has not be left off as the value of the "RedirectUrl" is a FULL url
itself, and since there is only one querystring value in this url "?" is
required instead of "&". In other words id=777 is not the QueryString of
www.Server1.com

Thanks for the suggestion anyway.

Mehdi



Charlie@CBFC said:
You left off "&" delimiter...

http://www.server1.com?RedirectUrl=http://www.server2.com&id=777

string url = Request.QueryString["RedirectUrl"]
string id = Request.QueryString["id"]

Charlie

Mehdi said:
Hi,

I get the following URL on page load:

http://www.server1.com?RedirectUrl=http://www.server2.com?id=777

So QueryString["RedirectUrl"] will return (after cleaning up via URL
Encode/decode) :
"http://www.server2.com?id=777"

However I am interested to get the value of "777" without using string
functions.

Any suggestions?



Kind Regards


Mehdi
 
C

Chris Fulstow

Hi,

Firstly, you might want to URL-encode the value of your RedirectUrl
parameter, some characters aren't allowed in the querystring, including
question marks. For more info about URL encoding and the
HttpUtility.HtmlEncode(String) function, see MSDN:
http://msdn2.microsoft.com/en-us/library/73z22y6h(VS.80).aspx

Once you've got the encoded value of RedirectUrl from the QueryString
collection, you might need to HtmlDecode() it to get back the original
values:
http://msdn2.microsoft.com/en-us/library/system.web.httputility.htmldecode(VS.80).aspx

Next, you need to get the querystring from you URL value. The easiest
way would be to use string functions to get everything to the right of
the "?", but you wanted to avoid this. Therefore, another approach
would be to create a Uri object from your URL string:
http://msdn2.microsoft.com/en-US/library/system.uri(VS.80).aspx

Uri myUrl = new Uri("http://www.server2.com?id=777");

You can now get the querystring part of the URL using the Uri.Query
property:

string myQuery = myUrl.Query;

Finally, how do you get the value of the id parameter without using
string functions? You can use the HttpUtility.ParseQueryString()
method to parse your querystring into a NameValueCollection object:
http://msdn2.microsoft.com/en-US/library/ms150046.aspx

NameValueCollection queryCol = HttpUtility.ParseQueryString(myQuery);
idValue = queryCol["id"];

Note that ParseQueryString() is only supported in .NET 2.0+.

Hope this helps,
Chris
 
M

Mehdi

Chris,

Thanks for the help. It seems that I will end up using string functions to
extract the id from MyUrl.Query. ParseQueryString() would have been the
perfect solution if I had .NET 2.


Regards

Mehdi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top