Minimal hosting
.NET 6 introduced the minimal hosting model. It combines the Startup
and Program
classes into a single Program.cs
file. It leverages top-level statements to minimize the boilerplate code necessary to bootstrap the application. It also uses global using directives and the implicit usings feature to reduce the amount of boilerplate code further. This model only requires one file with the following three lines of code to create a web application (remember you can use dotnet new web
to create an empty web application project):
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
app.Run();
The preceding code is way leaner than before. Of course, it starts an app that does nothing, but doing the same before would have required tens of lines of code.
The minimal hosting code is divided into two pieces:
- The web application builder we use to configure the application, register services, settings,...