Introducing Style Selector for New WebAPI Projects in .NET 7.0

New in .NET 7.0 [14]: Style selector for new WebAPI projects

The release of .NET 6.0 brought about a controversial requirement for all new WebAPI projects with ASP.NET Core to use the new API style called “Minimal APIs” without controller classes. Chief Technology Expert at MAXIMAGO, Dr. Holger Schwichtenberg, expressed some reservations over this decision due to its paternalistic nature. However, Microsoft has now given developers the choice again in .NET 7.0.

Now, when creating a new WebAPI project using the command line with dotnet new webapi, the default project will include a controller class within a file system folder named /Controllers. This means the new standard for WebAPI projects will be the old style instead of the Minimal APIs.

To use the Minimal API style, developers must explicitly add the –use-minimal-apis parameter when creating a new project using the command line. Meanwhile, in Visual Studio, the option is called “Use Controllers” and is active by default. Developers must uncheck this option to utilize the Minimal APIs.

There are now four templates available for WebAPIs in ASP.NET Core: Controller style with start class, Controller style with top-level statements, Minimal API with start class, and Minimal API with top-level statements. The top-level statements option is particularly interesting as it allows developers to combine both styles. By turning off “Do not use top-level statements,” the startup class Program and Main() method will no longer be present in the project.

In summary, Microsoft’s inclusion of choices in .NET 7.0 has provided developers with more flexibility when creating WebAPI projects using ASP.NET Core. While the Minimal API style may not be for everyone, developers can now choose which implementation is best suited to their needs.

Leave a Reply