Name

UrlEncode

Synopsis

returnstring = Server.UrlEncode(s)
Server.UrlEncode(s, output)

Returns a string in which any characters not allowed in URLs are encoded by using the URL literal equivalents, such as %2c for comma and + for space. This makes it very simple to pass any string as a query string value and, thanks to the new UrlDecode method, just as simple to retrieve the unencoded value.

Parameters

returnstring

A String variable to receive the encoded string from the method.

s

A String variable containing the value to be encoded by the method.

output

An instance of any class derived from the TextWriter class (found in the System.IO namespace); used to capture the encoded string. Classes derived from TextWriter include StringWriter and HtmlTextWriter.

Example

The code example declares two string variables, sets the value of StrToEncode, assigns the return value of the Server.UrlEncode call to StrToReturn, and then writes the HTML anchor tag containing a query string with the encoded value to the browser using the Message label control:

Sub Page_Load(  )
   Dim StrToEncode As String
   Dim StrToReturn As String
   StrToEncode = "Hello, World!"
   StrToReturn = Server.UrlEncode(StrToEncode)
   Message.Text = "<a href=""UrlDecode.aspx?StrToDecode=" & StrToReturn
   Message.Text &= """>" & StrToReturn & " - Click to Decode!</a>"
End Sub

Notes

This method replaces non-URL-allowable characters in strings that need to be passed as part of a URL. For example, one of the most difficult things to pass as part ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.