Posts

Localization in ASP.NET Core MVC with Example

Image
Using Query Strings for Request Culture and Simplifying Localization with IStringLocalizer Interface Let’s see how to implement Localization in ASP.NET Core MVC. In the first example, I am going to use the IStringLocalizer Interface in the controller to implement Localization. Additionally, when the user passes the desired language through the query string, it will display content in that language. First, let’s configure the settings in the program.cs file. builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");  The AddLocalization method adds the localization service to the service container. I have added the supported cultures here. I have added two cultures: en for English and nl for Dutch. In the RequestCultureProviders , I have included QueryStringRequestCultureProvider() . This allows the system to determine the culture information from the query string. UseRequestLocalization initializes a RequestLocalizationOptions objec

Database First Approach in Entity Framework Core: A Step-by-Step Guide

Image
Introduction: In this blog, I'll walk you through the process of setting up an Entity Framework Core project with a focus on Microsoft SQL Server as the database provider. Setting Up the Database: To begin, we created two tables, namely "Students" and "Teachers," in the "SampleDB" database. This forms the foundation for our data model. Creating the MVC Project: Next, we initiated a new MVC project using Visual Studio. Installing Necessary Packages: In the Package Manager Console, execute the following commands to install the essential packages for Entity Framework Core: Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design Install-Package Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.SqlServer: Installs the required package to enable Entity Framework Core with Microsoft SQL Server as th

Simplifying Code with Null Propagation in C#

Image
Code readability, maintenance, and reducing the risk of unexpected crashes. Earlier, developers used nested null checks to navigate through complex object structures, ensuring that each step along the way is not null. But WWith the introduction of Null Propagation in C#, a cleaner and simpler syntax appears, streamlining the process of handling null references. In this blog post, we'll explore Null Propagation using a simple code snippet. Before Null Propagation: Let's consider a scenario where we have a Person object and want to retrieve the city from their address. Here's how it might have been done traditionally: Person ? person = null ; string ? city = null ; if (person != null && person.Address != null ) { city = person.Address.City; }

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC

Image
Are you ready to enhance your ASP.NET Core MVC project with the power of Identity? In this comprehensive guide, we'll walk you through the process of setting up and customizing the Identity system in your web application. By the end of this tutorial, you'll be able to implement secure user authentication and authorization for your ASP.NET Core project. Step 1: Create a New ASP.NET Core Web App Begin by opening Visual Studio and selecting "ASP.NET Core Web App (Model-View-Controller)" as your project template. Follow these steps: 1. Open Visual Studio and create a new project, selecting "ASP.NET Core Web App (Model-View-Controller)". 2. In the "Configure your project" window, enter the project name and choose the project's location. 3. In the "Additional Information" window, select the framework and choose "Individual Accounts" as the Authentication type. 4. Click "Create" to generate your project.

Improving Code Readability in C# with Digit Separators

Image
What Are Digit Separators? Digit separators were introduced in C# 7.0. When dealing with large numbers, digit separators help enhance the readability of your code. By using underscore (_) as a separator within groups of numbers, you can significantly improve code readability. Why Use Digit Separators? Consider the scenario where you need to work with the number 1000000000. Without digit separators, reading this number can be challenging. However, by using digit separators and representing the number as 1_000_000_000, it becomes much easier to read one billion. Examples of Digit Separators Let's explore a few examples to understand how digit separators function: int totalPopulation = 7_900_000_000 ; double distanceToMoon = 384_400 . 0 ; long nationalDebt = 28_000_000_000_000 ; As evident, adding digit separators in between groups of digits has no impact on the numerical value itself. It only improves human readability. Conclusion The introduction of d

Simplify Your Code with Pattern Matching: Type Tests in C#

Image
Introduction: Pattern matching in C# is a powerful feature that simplifies common programming patterns and enables you to perform type tests and extract values from objects in a clear and brief way. In this blog post, you will explore the concept of type tests in pattern matching and demonstrate how they can simplify your code. Let's dive in! Understanding Type Tests in Pattern Matching: Type tests in pattern matching involve checking if an object is of a particular type and then extracting and working with the object in a type-safe manner. This eliminates the need for explicit casting and provides a more readable and maintainable solution. Let's illustrate this with an example: // Base shape class class   Shape  { } // Circle class class   Circle  :  Shape {      public   double   Radius  {  get ;  set ; } } // Rectangle class class   Rectangle  :  Shape {      public   double   Width  {  get ;  set ; }      public   double   Height  {  get ;  set ; } } // Triangle cl

Exploring Timer Triggers for Azure Functions

Image
Introduction: Azure Functions provide a serverless computing platform for executing code in a scalable and event-driven manner. One of the key triggers available in Azure Functions is the Timer trigger. With Timer triggers, you can schedule and automate the execution of your functions based on a specified time or interval. In this blog post, we will dive into the Timer trigger for Azure Functions and explore its features, use cases, and implementation. What is a Timer trigger in Azure Functions? The Timer trigger in Azure Functions allows you to schedule the execution of your functions at regular intervals or specific times. It is ideal for automating recurring tasks, periodic data processing, or triggering workflows based on a fixed schedule. With Timer triggers, you can define the timing using either a time expression or a more advanced cron expression. Key Features and Benefits Flexibility: Timer triggers support various time formats, including simple intervals, specific t