0

I generally use the code _uri = Server.UrlEncode(_uri); before any page redirection with parameter values but I don't know the significance of using UrlEncode for this.I want to know the benefit of using UrlEncode.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Gopal Biswas
  • 409
  • 1
  • 7
  • 34

3 Answers3

3

URLEncode returns a string in which all non-alphanumeric characters other than – (hyphen) _ (underscore), and . (period) have been replaced with a percent (%) sign followed by two hex digits that correspond to the charactor code and spaces encoded as plus (+) signs. Spaces can alternatively be encoded as %20. 20 is (16*2)+(1*0)=32 in decimal, which is the ASCII code for space.

Example

If You write

Response.Write(Server.URLEncode("http://www.technologycrowds.com"));

then Result will come like this

http%3A%2F%2Fwww%2Etechnologycrowdst%2Ecom
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Nitin Kumar
  • 898
  • 7
  • 22
3

Consider a URL like this:

http://example.com/index.aspx?redirectUrl=http://example.com/index.aspx?someValue=123

How would you parse that URL? Which :// separates the protocol from the address? Which ? separates the address form the values? It would get confusing fast, because the characters being used in the query string value have significant meanings in URLs.

Instead, consider this version:

http://example.com/index.aspx?redirectUrl=http%3A%2F%2Fexample.com%2Findex.aspx%3FsomeValue%3D123

Now none of the characters being used in the query string value have other significant URL meanings. It's just a single string, so the entire URL can be parsed easily.

URL-encoding values allows us to treat those values as values, and not as parts of the URL itself.

David
  • 208,112
  • 36
  • 198
  • 279
0

URL encoding ensures that all browsers will correctly transmit text in URL strings.Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers.

See Microsoft documentation: https://msdn.microsoft.com/en-us/library/zttxte6w(v=vs.110).aspx

Andrea
  • 112
  • 1
  • 13