WebUtility.UrlEncode(String) Method

Definition

Converts a text string into a URL-encoded string.

public static string UrlEncode (string value);
public static string? UrlEncode (string? value);

Parameters

value
String

The text to URL-encode.

Returns

A URL-encoded string.

Remarks

If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding replaces characters that are not allowed in a URL with character-entity equivalents consisting of hexadecimal escape sequences. The converted string is expected to conform to the UTF-8 format.

URL encoding replaces all character codes except for letters, numbers, and the following punctuation characters:

  • - (minus sign)

  • _ (underscore)

  • . (period)

  • ! (exclamation point)

  • * (asterisk)

  • ( and ) (opening and closing parentheses)

Each ' ' (space) character is converted to a + (plus) character.

Note

Replacing ' ' (space) with a '+' (plus) sign is expected only in query part of URI (by convention, not by standard) or in application/x-www-form-urlencoded HTTP content. Moreover, the above mentioned list of characters that are not replaced does not correspond neither to URL specification nor URI RFC 3986. Use EscapeDataString for RFC compliant transformation.

For example, when embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

The UrlDecode method reverses the encoding.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0