Search code examples
c#asp.neturlurlencodeurl-encoding

Why we use UrlEncode


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.


Solution

  • 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