var url = @"http://example.com/resource?foo=bar with space#fragment";
var httpUtilityEncoded = HttpUtility.UrlEncode(url);
Console.WriteLine(httpUtilityEncoded); //http%3a%2f%2fexample.com%2fresource%3ffoo%3dbar+with+space%23fragment
var httpUtilityDecoded = HttpUtility.UrlDecode(httpUtilityEncoded);
Console.WriteLine(httpUtilityDecoded); //http://example.com/resource?foo=bar with space#fragment
These methods take a single string parameter containing the URL to be either encoded or decoded. By default, these methods use a UTF-8 encoding, but if this is not the case, there is an overload to pass a different encoding instead. There are also other method overloads to pass a Byte[] instead of a string type.
How to Encode and Decode Using the WebUtility Class
The documentation states that if we are not within a web application, we should use the WebUtility class to perform URL encoding and decoding instead. This class is in the Sytem.Net namespace.
Usage is very similar to the previous examples, although there are no overloads:
Wanna join Code Maze Team, help us produce more awesome .NET/C# content and
get paid? >> JOIN US! <<
var webUtilityEncoded = WebUtility.UrlEncode(url);
Console.WriteLine(webUtilityEncoded); //http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar+with+space%23fragment
var webUtilityDecoded = WebUtility.UrlDecode(webUtilityEncoded);
Console.WriteLine(webUtilityDecoded); //http://example.com/resource?foo=bar with space#fragment
How to Encode and Decode Using the Uri Class
Alternatively, we can use the Uri class to encode and decode URLs. Instead of UrlEncode() and UrlDecode(), the methods are called EscapeDataString() and UnescapeDataString():
var uriEncoded = Uri.EscapeDataString(url);
Console.WriteLine(uriEncoded); //http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar%20with%20space%23fragment
var uriDecoded = Uri.UnescapeDataString(uriEncoded);
Console.WriteLine(uriDecoded); //http://example.com/resource?foo=bar with space#fragment
Differences Between the Different Options
If we look closely at the output from the examples, we’ll notice that HttpUtility.UrlEncode() produces lowercase encoding of reserved characters, whilst WebUtility.UrlEncode and Uri.EscapeDataString both output uppercase. So a ‘?‘ character encodes to either ‘%3f‘ or ‘%3F‘.
The way the space character is encoded also differs between these implementations. HttpUtility.UrlEncode and WebUtility.UrlEncode both encode a space character to ‘+‘, whereas Uri.EscapeDataString encodes a space as ‘%20‘ instead.
If we look at how these methods encode other characters like ‘!‘, ‘(‘, ‘)‘, ‘*‘, and ‘~‘ we’ll see there are also differences, so this might influence which implementation we choose to use.
Another consideration is that there is a limit of 32766 characters for Uri.EscapeDataString. If we try and encode more characters than that limit, it will throw a UriFormatException – so if we do need to encode a particularly long URL, we’ll probably want to use either HttpUtility.UrlEncode or WebUtility.UrlEncode instead.
Conclusion
We’ve learned about URL encoding and discovered there are multiple implementations in .NET to perform both encoding and decoding. These methods differ slightly in how they encode and decode our URLs. The particular implementation we choose will depend upon our specific requirements.