Minimal Hosting– Minimal API

.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:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Run();

Let’s call that way leaner than before. Of course, the preceding code 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:

That simplified model led to minimal APIs that we explore next.

Minimal APIs

ASP.NET Core’s Minimal APIs are built on the minimal hosting model and bring a lean approach to constructing web applications. Highly inspired by Node.js, they facilitate the development of APIs by reducing the boilerplate code. By emphasizing simplicity and performance, they enhance readability and maintainability. They are an excellent fit for microservices architecture and applications that aim to remain lean.

You can also build large applications using Minimal APIs; the word minimal refers to their lean approach, not the type of application you can make with them.

This minimalist approach does compromise a little on functionalities but improves flexibility and speed, ensuring you have complete control over your API’s behavior while keeping your project lean and efficient. Minimal APIs include the necessary features we need for most applications, like model binding, dependency injection, filters, and a route-to-delegate model. If you need all the features from MVC, you can still opt to use MVC. You can even use both; this is not one or the other.

We explore the Model-View-Controller (MVC) pattern in Chapter 6, MVC.Let’s have a look at how to map routes next.

You may also like