Encode and Decode URLs in ASP.NET Core C#

.NET CORE | How to Encode and Decode URLs in ASP.NET Core C#

What is URI Encoding

It is the process of converting certain characters to a specific format to make them valid for use in a URI. This conversion is necessary before transmitting these characters over the internet. In this article, we will learn How to Encode and Decode URLs in ASP.NET Core C#.

URLs can be categorized into reserved and unreserved characters. The reserved characters have special meanings, such as the ‘?’ character, which indicates the beginning of query parameters.

Encode and Decode URLs using the WebUtility Class in ASP.NET Core C#

The “WebUtility” class, which is available in the ‘System.Net’ namespace.

The “WebUtility.UrlEncode()” and “WebUtility.UrlDecode” methods will encode and decode the URI.

Encode URI

 var urlsWeb = "https://labpys.com/path/page?param=value";
            var encodedWeburls = WebUtility.UrlEncode(urlsWeb);
            Console.WriteLine($"Encode URL using Web Utility {encodedWeburls}");
 https%3A%2F%2Flabpys.com%2Fpath%2Fpage%3Fparam%3Dvalue

Decode URI

var encodedWeburls ="https%3A%2F%2Flabpys.com%2Fpath%2Fpage%3Fparam%3Dvalue";
var decodedurlsWeb = WebUtility.UrlDecode(encodedWeburls);
Console.WriteLine($"Decode URL using Web Utility {decodedurlsWeb}");
https://labpys.com/path/page?param=value

Encode and Decode URI using HttpUtility

To access the “HttpUtility” class include the “System.Web” namespace. The ‘HttpUtility.UrlEncode()’ and ‘HttpUtility.UrlDecode()’ method to encode and decode the URI, takes a single string parameter containing the URL to encode and decode. Here’s an example in C#:

Encode URI

   var urls = "https://labpys.com/path/page?param=value";
   var encodedurls = HttpUtility.UrlEncode(urls);
   Console.WriteLine($"Encode URL using HttpUtility  {encodedurls}");
   // output -  https%3a%2f%2flabpys.com%2fpath%2fpage%3fparam%3dvalue           

Decode URI

var decodedurlspath = HttpUtility.UrlDecode(encodedurls);
Console.WriteLine($"Decode URL using HttpUtility  {decodedurlspath}");
//output - https://labpys.com/path/page?param=value

Encode and Decode using Uri Class

Alternatively, we can use the Uri class to encode and decode URLs. The “Uri.EscapeDataString()’ and “Uri.UnescapeDataString()” methods encode and decode the URI.

             var urlsUri = "https://labpys.com/path/page?param=value";
            var encodedUriurls = Uri.EscapeDataString(urlsUri);
            Console.WriteLine($"Encode URL using Uri Class  {encodedUriurls}");
            //result - https%3A%2F%2Flabpys.com%2Fpath%2Fpage%3Fparam%3Dvalue

            var decodedurlsUri = Uri.UnescapeDataString(encodedUriurls);
            Console.WriteLine($"Decode URL using Uri Class {decodedurlsUri}");
            //result -https://labpys.com/path/page?param=value

More Articles:

How to Enable CORS in ASP.NET CORE WEB API

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments