The URLEncode method applies URL encoding rules, including escape characters, to a specified string. A URL-encoded string may be passed in a URL as a query string argument.
Page.URLEncode(String)
Strings that contain certain characters such as spaces, slashes (/) or percentage symbols (%) cannot be passed as URL query arguments. URL encoding encodes these characters as escaped text so that they can be passed in a URL.
The Page.URLEncode method is equivalent to Server.URLEncode, except that the Page.URLEncode method handles Null values by returning an empty string (Server.URLEncode fails when the String parameter is Null). This distinction is important because a database queries can return Null.
Suppose that a variable called referringURL
contains the URL http://www.microsoft.com
, which is to be passed as a query string argument in another URL.
The following script:
<A HREF="Sample.asp?referringURL=<% = mscsPage.URLEncode(referringURL) %>">My link</A>
generates the following HTML:
<A HREF="Sample.asp?referringURL=http%3A%2F%2Fwww%2Emicrosoft%2Ecom">My link</A>
The receiving page (Sample.asp) can use the Request method to get the value of the variable. The method converts the string back to its original form and returns it to the script on the page.