What is the role of Program.cs (and the old Startup class) in ASP.NET Core?
Quick Answer
Program.cs is the application's entry point: it builds the host, registers services in the DI container (builder.Services), and configures the middleware pipeline (app.Use...), then runs the app. The old Startup class split this into ConfigureServices and Configure; since .NET 6 it's one top-level file.
Program.cs is where the app starts. It does two jobs: everything before builder.Build() registers services in the DI container, and everything after it wires up the middleware pipeline and maps the endpoints, then app.Run() starts listening. In older projects that was the Startup class with its ConfigureServices and Configure methods; the minimal hosting model in .NET 6 flattened it into one file, but it's the same two phases.
WebApplication.CreateBuilder(args)sets up configuration, logging, DI and the host.- Before
builder.Build(): add services (AddControllers,AddDbContext, your own registrations). - After
builder.Build(): compose the pipeline in order, then map endpoints, thenapp.Run(). - The two-phase shape (services first, then pipeline) is deliberate — the pipeline resolves services from the container that services registration populated.