Posts

How to use multiple appsettings.json files in .net core webapi project for different environments

Image
Managing configuration is one of the most important parts of any .NET Core application. ASP.NET Core provides a powerful configuration system that allows you to load multiple configuration files, override settings per environment, and keep secrets out of source control. Why Use Multiple AppSettings Files? Advantages 1. Cleaner separation between environments Each environment has its own config file: dev database test API URLs production connection strings 2. No need to modify code during deployment Switch environment → settings automatically apply. 3. Safe and secure deployments Production secrets never appear in development files. 4. Works cleanly with CI/CD & Docker Different settings per environment, same codebase. 5. Supporting reloadOnChange You can update JSON settings without restarting the app (in Development). Disadvantages 1. Requires proper environment variable setup If ASPNETCORE_ENVIRONMENT is not set correctly, the wrong config loads. 2. Mo...

Extension Methods in C# | Add New Functionality Without Modifying Code (.NET Tutorial)

Image
What Are Extension Methods? Extension methods enable you to add new methods to existing types without modifying the original type, recompiling, or otherwise changing the type.  In C#, an extension method allows you to "add" new methods to an existing class — without modifying the original class or creating a derived type. It is a powerful way to make your code more readable and modular , especially when you’re working with third-party libraries or base .NET types you can’t change. Syntax and Basic Example // Step 1: Create a static class public static class StringExtensions {     // Step 2: Create a static method with 'this' before the first parameter     public static bool IsNullOrEmpty(this string value)     {         return string.IsNullOrEmpty(value);     } } // Step 3: Use the extension method class Program {     static void Main()     {         string name = null;   ...

Minimal APIs in .NET: Build Fast, Lightweight Web Services

Image
What is Minimal API in .NET? Minimal APIs are  a simplified, high performance approach for building HTTP APIs with ASP.NET core that require minimal code and configuration. Instead of the traditional, controller-based method, minimal APIs allow developers to define API endpoints directly in their project's primary file, such as program.cs This approach, first introduced in .NET 6, is ideal for scenarios where a full-fledged MVC framework would be overkill, such as for microservices, serverless functions, and lightweight applications Characteristics Reduced boilerplate:  A minimal API avoids the traditional scaffolding of controllers, action methods, and attributes, resulting in a more concise and streamlined codebase. Concise syntax:  Endpoints are declared using a functional style with lambda expressions and the map extension methods (mapget, mappost etc.), making the code brief and focused. High performance:  With fewer layers of abstraction and middlewa...

Understanding Middleware in .NET Core

Image
What is Middleware in ASP.NET Core? If you're starting to learn ASP.NET Core , you've probably heard the word "middleware" quite a few times. It might sound complicated at first, but don’t worry — in this post, we’ll break it down into simple terms . You’ll learn: What middleware is Why it's important How it works How to create your own Best practices (and things to avoid) What is Middleware in .NET Core? Middleware is a piece of code that sits between the incoming HTTP request and the outgoing HTTP response in an ASP.NET Core app. It can: Inspect or modify the request Handle authentication or logging Pass the request to the next step Or even stop the request entirely In simple words: Middleware is like a checkpoint that performs some logic on every request before it reaches your controller or API. Real-Life Analogy: The Airport Imagine you're at an airport going through various checks: Security Check (Middleware 1): Checks...