views:

240

answers:

3

Hi, I have a page where I am recieving an url in a parameter like this:

www.example.com?url=www.myurl.com?urlparameter1=hey

The problem is that when I try to retrieve the parameter "url", I only receive "www.myurl.com" instead of "www.myurl.com?urlparameter1=hey". What is the best way to receive the url, is it to extract the whole url and remove www.example.com or is there a more efficient way?

+5  A: 

You need to URL encode your URL parameter, otherwise it will be read as separate querystring parameters.

Use HttpUtility.UrlEncode.

Do this: string urlParameter = HttpUtility.UrlEncode("www.myurl.com?urlparameter1=hey");

That will give you: www.myurl.com%3furlparameter1%3dhey

http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

Scott Lance
Hmm the wierd this is I just tried it, but it didn't work. I tried it again now, and now it worked. I might have misplaced the encoding before.
Dofs
Thank you. Helped me too.
NLV
+1  A: 

I'm not convinced this is a legal url (though I'm sure it will work in most cases)...you haven't urlencoded your characters like the '?' and the '=' in your parameter. Ideally, you can just use to use HttpServerUtility.UrlEncode to do this, if you're the one generating these urls.

...but if you can't do that for some reason, you'll probably have to roll your own solution, as you mentioned, by extracting them manually from the whole url.

Beska
A: 

The parameter and page are separated by "?", but the subsequent parameters are separated by "&". If you are manually creating that querystring, fix it.

If you meant to have the question mark and equals sign as part of the value for the URL parameter, they should be URL-encoded.

JasonTrue