🔹 Program.cs
in ASP.NET Core MVC
The Program.cs
file serves as the entry point for an ASP.NET Core MVC application. It configures and starts the web server, sets up services, and defines the HTTP request pipeline.
Unlike traditional ASP.NET, every ASP.NET Core application starts like a console application and transforms into a web application. When you press F5 to run the application, execution begins with the Program.cs
file.
đź§© Default Program.cs
in ASP.NET Core 7 (MVC)
🔸 This file uses top-level statements introduced in C# 9, which is why there's no explicit
Main()
method.
đź§± Simplified Structure
âś… Step-by-Step Explanation
1. Create Builder
-
Initializes the app with default settings.
-
Configures the internal web server (Kestrel), sets the content root, and loads
appsettings.json
.
2. Register Services (Dependency Injection)
-
Adds MVC services to the dependency injection container.
-
Prepares the app to use models, views, and controllers.
3. Build the App
-
Builds the app and prepares the middleware pipeline.
4. Configure Middleware Pipeline
-
Adds error-handling middleware that redirects to
/Home/Error
in non-development environments.
5. Configure Routing
-
Defines the default route for MVC:
-
Controller =
Home
-
Action =
Index
-
id
is optional
-
6. Run the App
-
Starts the application and begins listening for incoming HTTP requests.
📝 Summary
The Program.cs
file in ASP.NET Core MVC sets up all the infrastructure required to run the application:
-
Initializes the web server
-
Configures services and DI
-
Sets up middleware
-
Defines routing rules
-
Starts the application