🔹 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)

 var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.
    builder.Services.AddControllersWithViews();

    var app = builder.Build();

    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

    app.Run();

🔸 This file uses top-level statements introduced in C# 9, which is why there's no explicit Main() method.


đź§± Simplified Structure

     var builder = WebApplication.CreateBuilder(args);

        // Register services here

        var app = builder.Build();

        // Configure middleware here

        app.Run(); // Start the application

âś… Step-by-Step Explanation

1. Create Builder

var builder = WebApplication.CreateBuilder(args);
  • 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)

  builder.Services.AddControllersWithViews();
  • Adds MVC services to the dependency injection container.

  • Prepares the app to use models, views, and controllers.

3. Build the App

 var app = builder.Build();
  • Builds the app and prepares the middleware pipeline.

4. Configure Middleware Pipeline

    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
    }
  • Adds error-handling middleware that redirects to /Home/Error in non-development environments.

    app.UseStaticFiles();    // Serve static files from wwwroot
    app.UseRouting();        // Enable routing
    app.UseAuthorization();  // Enable authorization

5. Configure Routing

    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
  • Defines the default route for MVC:

    • Controller = Home

    • Action = Index

    • id is optional

6. Run the App

app.Run();
  • 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

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions