Search⌘ K
AI Features

ASP.NET Core MVC Initialization

Explore how to initialize and configure an ASP.NET Core MVC web application. Understand the setup of Program.cs, dependency injection, routing, middleware, and default MVC routes to create scalable websites.

Appropriately enough, we will start by exploring the MVC website’s default initialization and configuration:

Step 1: Open the Program.cs file and note that it uses the top-level program feature (so there is a hidden Program class with a $ method). This file can be divided into four important sections from top to bottom. As we review the sections, we might want to add comments to remind ourselves what each section is used for.

Note: .NET 5 and earlier ASP.NET Core project templates used a Startup class to separate these parts into separate methods, but with .NET 6 and later, Microsoft encourages putting everything in a single Program.cs file.

Step 2: The first section imports some namespaces, as shown in the following ...

C#
// Section 1 - import namespaces
using Microsoft.AspNetCore.Identity; // IdentityUser
using Microsoft.EntityFrameworkCore; // UseSqlServer, UseSqlite
using Northwind.Mvc.Data; // ApplicationDbContext
...