2

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?

3 Answers 3

6

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

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

1 Comment

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.
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.