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

In addition to the paternalism mentioned in the previous part of the series, .NET 6.0 made another requirement that not everyone liked: All new WebAPI projects with ASP.NET Core used the new API style in .NET 6.0 ” Minimal APIs” without controller classes.




dr Holger Schwichtenberg is Chief Technology Expert at MAXIMAGO, which offers innovation and experience-driven software development, including in highly critical safety-related areas. He is also head of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with advice and training in the development and operation of software with 38 renowned experts.

In .NET 7.0, Microsoft gives you the choice again. At the command line arises at

dotnet new webapi

by default in the new project again a file system folder /Controllers with a controller class



The new standard for WebAPI projects is back to the old style (Fig. 1).

The minimal APIs only exist if you explicitly use the parameter --use-minimal-apis adds:

dotnet new webapi --use-minimal-apis

In Visual Studio, the option is called the other way around: “Use Controllers”. However, it is active by default.



in Visual Studio you have to uncheck “Use Controllers” for the minimal APIs (Fig. 2).

Figure 3 shows two WebAPI projects:

  • On the left is a minimal API, but with a start class instead of top-level statements. A waiver class Program and Main() would be possible by disabling “Do not use top-level statements”.
  • On the right is the classic controller style with a separation between the start class and the controller class.



Minimal API vs. Controller style – each with class Program and Main (Fig. 3)

Both styles can now be combined with the “Do not use top-level statements” option. If you turn off “Do not use top-level statements” you don’t get a starting class Program and no static Main()-Method more (see Figure 4). So in total there are now four templates for ASP.NET Core-based WebAPIs:

  • controller style with start class,
  • Controller-Stil mit Top-Level-Statements,
  • Minimal API with startup class
  • Minimal-API mit Top-Level-Statements.



Minimal API versus Controller style – each with top-level statements (Fig. 4)


(rme)

To home page

Related Posts

Hot News

Trending

usefull links

robis robis robis