Understanding the Program.cs – The Minimal Hosting Model

Advanced Updated June 17, 2025

🧱 Understanding the Program.cs – The Minimal Hosting Model

In .NET 6 and beyond, the hosting model is simplified. Program.cs combines setup and configuration:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Key concepts:

  • AddControllers() adds support for MVC-style controllers.
  • MapControllers() maps attribute-routed controllers to endpoints.
  • Swagger provides interactive API documentation.