Url.Encode method problem

TheCommunicator
TheCommunicator used Ask the Experts™
on
Hi Guys,

This is the first time I am using this URL.Encode method and it does not seem to work properly in a manner that it does not load the URl. I am not sure what the problem is.  

For example, I have this Original URL:

https://checkout.google.com/view/buy?o=shoppingcart&shoppingcart=551005444635424

Open in new window


Because this is C#, I have to do a string literal  and have to replace "/" with "//"

so it becomes:

https:////checkout.google.com//view//buy?o=shoppingcart&shoppingcart=551005444635424

Open in new window


Now, when i encode it then it becomes:

https%3a%2f%2f%2f%2fcheckout.google.com%2f%2fview%2f%2fbuy%3fo%3dshoppingcart%26shoppingcart%3d551005444635424

Open in new window



and that does not work.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
You are only supposed to encode the query string after the question mark.

Author

Commented:
so in this URL i would not encode the part up to

https://checkout.google.com/view/buy

Open in new window


and then encode

?o=shoppingcart&shoppingcart=551005444635424

Open in new window


including question mark?

And how about string literals? should they be encoded with "//" or "/"?



Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Not including the question mark.  I don't know about the string literals.  Nothing I use requires that.  PHP and javascript can require backslashes '\' to be escaped in some circumstances.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Thanks guys:)

Just wondering though that I thoughr Server.UrlEncode shouldn't be smart enough to recognized that what to encode and what NOT to encode when we pass it a URL as it is?

Why do we have to separate all the query parameters? It sounds like an ineffecient function, isn't it?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
I don't like 'Smart' functions if they are the only way to do things.  You're not supposed to pass a complete URL thru it.  The query parameters normally start out as separate items that are added to the URL.  URLencode works this same way in the other languages I'm aware of.  In javascript and PHP, it is customary to encode each query item before concatenating them to the URL.

The entire internet is an inefficient function.

Author

Commented:
Thank you so much:)
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
You're welcome, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial