inAsp.net Core Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Mon, 30 Oct 2023 06:42:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 inAsp.net Core Archives - Tech Insights 32 32 230003556 .NET CORE | How to Encode and Decode URLs in ASP.NET Core C# https://reactconf.org/net-core-how-to-encode-and-decode-urls-in-asp-net-core-csharp/ https://reactconf.org/net-core-how-to-encode-and-decode-urls-in-asp-net-core-csharp/#respond Mon, 30 Oct 2023 06:42:31 +0000 https://labpys.com/?p=2024 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 …

The post .NET CORE | How to Encode and Decode URLs in ASP.NET Core C# appeared first on Tech Insights.

]]>
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

The post .NET CORE | How to Encode and Decode URLs in ASP.NET Core C# appeared first on Tech Insights.

]]>
https://reactconf.org/net-core-how-to-encode-and-decode-urls-in-asp-net-core-csharp/feed/ 0 2024