This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

Encodes a URL string. These method overloads can be used to encode the entire URL, including query-string values.

To encode or decode values outside of a web application, use the WebUtility class.

public:
 static System::String ^ UrlEncode(cli::array <System::Byte> ^ bytes, int offset, int count);
public static string? UrlEncode (byte[]? bytes, int offset, int count);
public static string UrlEncode (byte[] bytes, int offset, int count);
static member UrlEncode : byte[] * int * int -> string
Public Shared Function UrlEncode (bytes As Byte(), offset As Integer, count As Integer) As String

Parameters

Remarks

The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

To encode or decode values outside of a web application, use the WebUtility class.

public:
 static System::String ^ UrlEncode(System::String ^ str, System::Text::Encoding ^ e);
public static string? UrlEncode (string? str, System.Text.Encoding e);
public static string UrlEncode (string str, System.Text.Encoding e);
static member UrlEncode : string * System.Text.Encoding -> string
Public Shared Function UrlEncode (str As String, e As Encoding) As String

Parameters

Remarks

This method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

To encode or decode values outside of a web application, use the WebUtility class.

public:
 static System::String ^ UrlEncode(cli::array <System::Byte> ^ bytes);
public static string? UrlEncode (byte[]? bytes);
public static string UrlEncode (byte[] bytes);
static member UrlEncode : byte[] -> string
Public Shared Function UrlEncode (bytes As Byte()) As String

Parameters

Remarks

The UrlEncode method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

To encode or decode values outside of a web application, use the WebUtility class.

public:
 static System::String ^ UrlEncode(System::String ^ str);
public static string? UrlEncode (string? str);
public static string UrlEncode (string str);
static member UrlEncode : string -> string
Public Shared Function UrlEncode (str As String) As String

Parameters

Remarks

The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream without encoding, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when the characters < and > are embedded in a block of text to be transmitted in a URL, they are encoded as %3c and %3e.

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

The HttpUtility.UrlEncode method uses UTF-8 encoding by default. Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter.

UrlEncode is a convenient way to access the UrlEncode method at run time from an ASP.NET application. Internally, UrlEncode uses the UrlEncode method to encode strings.

To encode or decode values outside of a web application, use the WebUtility class.