问题
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?
回答1:
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
回答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.
回答3:
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.
来源:https://stackoverflow.com/questions/1380617/request-url-parameter