.NET 6 has taken simplicity to a whole new level, goodbye boilerplate. What are Minimal APIs? The clue is in the name…minimal APIs are architected to create HTTP APIs with minimal dependencies. When would you use it? They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.
So…how much effort is really involved in creating a minimal API, with swagger documentation? I took to Visual Studio 2022 Preview to find out. Firstly I created an ASP .NET Core Empty project. I found out alternatively you can use the command `dotnet new web` in the Dotnet CLI. This created me the default `Program.cs`, with the “hello world” example.
In order to compare a standard ASP.NET API to a minimal API, I chose the classic Weather Forecast as an example. As you can see this was achieved with negligible lines of code, with hardy any effort. This was included in the C# 9 Top-level programs feature, where all the logic, architecture, classes, endpoints and services are in one file. In order to use swagger I simply had to install the `Swashbuckle.AspNetCore` nuget, then edit the Program file.
Again, comparing the old way to minimal APIs, you can see previously we needed to use a Controller, Program, Start up and Model where as now we just need a Program file…one class to rule them all! I have uploaded this basic example to my github.
This light weight starting point has huge advantages, especially in the microservice world, when creating proof of concepts or even when doing TDD, just don’t forget to refactor! With C# being a mature language, the idea is that it just works out of the box, whether you want your API to return a string or a model. Personally I have only worked a few .NET Core solutions so far therefore it is fair to say I am more familiar with the “classic” layout. As well as this I’m still getting used to the out of the box DI, instead of using Ninject or Castle Winsor. I can’t imagine this being an adopted feature in enterprise applications, with the lack of separation, DI in one location and configuration.