Code snippet illustrating the UseRewriter middleware in ASP.NET Core

Handling Duplicate URLs in ASP.NET MVC: www vs non-www

Concerned about duplicate URLs in ASP.NET MVC? Take care of both www and non-www domains. .NET already provides the correct method to handle them. Using the UseRewriter middleware is the optimal approach. You can use AddRedirectToNonWww() or AddRedirectToWww() for this purpose. 馃憠 Adapted from my original post on LinkedIn .

July 15, 2024 路 1 min 路 50 words 路 Eduardo Potumati
Code snippet demonstrating how to use Reflection to read custom attributes in C#

Accessing Custom Attributes Using Reflection in C#

Accessing Custom Attributes Using Reflection in C# Custom attributes provide a powerful way to attach declarative metadata to your code-whether it鈥檚 on classes, methods, properties, or fields. But how do you actually read that metadata at runtime? This is where Reflection shines. By using the System.Reflection, you can dynamically inspect your code鈥檚 structure and retrieve the information defined within those custom attributes. This functionality is the backbone for creating highly flexible applications, such as automated validation frameworks, custom JSON serializers, or dynamic API routing systems. ...

June 15, 2024 路 1 min 路 116 words 路 Eduardo Potumati
Code snippet demonstrating pattern matching to protect against nulls in C#

Pattern Matching in C#: Getting Protected from Nulls

Pattern Matching: Get Protected from Nulls C# pattern matching provides a substantially more concise syntax for testing expressions and taking decisive action when an expression matches. By leveraging these structural features, you can gracefully bundle type checks, variable declarations, and null validations into a single, highly readable statement-significantly reducing boilerplate code and the risk of unexpected NullReferenceExceptions. 馃憠 Adapted from my original post on LinkedIn .

May 15, 2024 路 1 min 路 66 words 路 Eduardo Potumati
Code example illustrating Regex route constraints in ASP.NET Core MVC

Using Regular Expressions for ASP.NET MVC Routing

Using Regular Expressions (Regex) is a highly effective method for segmenting your ASP.NET MVC routing. With Regex route constraints, developers can create intricate matching patterns, leading to much more flexible and dynamic routing configurations directly in their controllers. Important: Always exercise caution when using System.Text.RegularExpressions to process untrusted input, as poorly constructed patterns can be vulnerable to Regular Expression Denial of Service (ReDoS) attacks. 馃憠 Adapted from my original post on LinkedIn . ...

April 15, 2024 路 1 min 路 73 words 路 Eduardo Potumati
Code snippet demonstrating EF.Functions.Collate for accent-insensitive querying in EF Core

Handling Accents in SQL with EF Core

Have you ever needed to handle accents in SQL? If you are Latin, you have certainly done it many times. And how does it work in EF Core? It鈥檚 easy, you just have to use EF.Functions.Collate! Warning: Overriding case-sensitivity in a query via EF.Functions.Collate (or by calling string.ToLower) can have a very significant impact on your application鈥檚 performance. 馃敆 Know more 馃憠 Adapted from my original post on LinkedIn . ...

March 15, 2024 路 1 min 路 70 words 路 Eduardo Potumati
Code snippet demonstrating the differences between Round, Floor, and Ceiling in C#

Differences Between Round, Floor, and Ceiling in C#

Do you know the differences between Round, Floor, and Ceiling in C#? Round: It follows standard mathematical rounding to the nearest integer. For example, 5.67 => 6, and 5.47 => 5. Floor: It rounds the number down to the nearest integer. For example, 5.67 => 5, and 5.47 => 5. Ceiling: It rounds the number up to the nearest integer. For example, 5.67 => 6, and 5.47 => 6. 馃憠 Adapted from my original post on LinkedIn . ...

February 15, 2024 路 1 min 路 78 words 路 Eduardo Potumati
Code diagram comparing struct and class memory allocation in .NET

Struct or Class: When to Use Which in C#

Struct or class? Most types in a framework should be classes, but there are some situations in which the characteristics of a value type make it more appropriate to use structs. Here are some things to consider: 馃憤 If your instances are small and short-lived, or commonly embedded in other objects, consider using a struct. 馃憥 However, be cautious when defining a struct. Only use it if the type represents a single value, is immutable, has an instance size under 16 bytes, and will not need to be boxed frequently. ...

December 15, 2023 路 1 min 路 125 words 路 Eduardo Potumati