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.
- 38,279
- 56
- 171
- 280
- 387
- 1
- 5
- 32
-
1it will encode any special characters like &,= in the values of the parameters – Ehsan Sajjad Sep 01 '16 at 12:36
-
What is the benefit of doing this encoding? @Ehsan Sajjad – Gopal Biswas Sep 01 '16 at 12:39
-
1If you don't use it, you could have characters in your URL that are interpreted in the wrong way. It is somewhat like putting quotes around a string to tell the compiler/server not to worry about what's inside. – Shannon Holsinger Sep 01 '16 at 12:39
3 Answers
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
- 60,736
- 15
- 100
- 154
- 878
- 6
- 22
-
-
you can refer to http://stackoverflow.com/questions/4667942/why-should-i-use-urlencode – Nitin Kumar Sep 01 '16 at 12:59
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.
- 188,958
- 33
- 188
- 262
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
- 112
- 1
- 13