ASP.NET Core Interview Question #4

What is the role of Program.cs in an ASP.NET Core application?

ASP.NET Core Fundamentals Junior Beginner

Quick Interview Answer

Program.cs is the application entry point. In the modern hosting model, it creates the WebApplicationBuilder, registers services in the dependency injection container, builds the application, configures the HTTP request pipeline, maps endpoints, and starts the application.

Detailed Explanation

Program.cs is the application entry point. In the modern hosting model, it creates the WebApplicationBuilder, registers services in the dependency injection container, builds the application, configures the HTTP request pipeline, maps endpoints, and starts the application.

Because service registration and middleware ordering both occur here, Program.cs is one of the most important files for understanding an ASP.NET Core application's runtime behavior.

Code Example

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapDefaultControllerRoute();
app.Run();