In C# 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 In C# 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
How to Return an Empty Collection in C# https://reactconf.org/how-to-return-an-empty-collection-in-csharp/ https://reactconf.org/how-to-return-an-empty-collection-in-csharp/#respond Fri, 20 Oct 2023 06:41:58 +0000 https://labpys.com/?p=1974 In C#, when returning a collection or array, It is better to return empty collections or a collection instance with no items. Returning an empty collection eliminates the need to …

The post How to Return an Empty Collection in C# appeared first on Tech Insights.

]]>
In C#, when returning a collection or array, It is better to return empty collections or a collection instance with no items. Returning an empty collection eliminates the need to check for null before iterating over the items in the collection, resulting in a cleaner method calling code.

The System.Linq.Enumerable class has a useful generic method called Empty. This method returns an empty instance of IEnumerable<T> (where T is your type parameter).

Here is an example of a return Empty Collection in C#:

        [HttpGet("getemployee")]
        public async Task<IEnumerable<Employee>> GetEmployees()
        {
            var results = this._employeeRepo.GetEmployeeListAsync();
            if(results.AsyncState == null)
            {
                return Enumerable.Empty<Employee>();
            }
            return await results;
        }

Output

If the result set returns a Null value, then “IF” condition is executed otherwise it returns collection items.

Return an Empty Collection in C#

Recommended Articles:

The post How to Return an Empty Collection in C# appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-return-an-empty-collection-in-csharp/feed/ 0 1974