Why is 'dotnet new webapi' command not producing the controllers folder?

1.2k Views Asked by At

Upon using dotnet new webapi -n API it produces the API folder with the properties, obj and bin subfolders as in the screenshot below, but it doesn't have a Controllers folder. Can anyone guide me on this? The project is using .NET 8.0

Image1

2

There are 2 best solutions below

1
On BEST ANSWER

It seems to be a recent .NET 8 change resulting in the Minimal APIs used by default.

From the docs for dotnet new templates: webapi (with fixed typo - PR @github):

  • -minimal|--use-minimal-apis
    Create a project that uses the ASP.NET Core minimal API. Default is false, but this option is overridden by --controllers. Since the default for --controllers is false, entering dotnet new webapi without specifying either option creates a minimal API project.
  • -controllers|--use-controllers
    Whether to use controllers instead of minimal APIs. If both this option and -minimal are specified, this option overrides the value specified by -minimal. Default is false. Available since .NET 8 SDK.

So add -controllers/--use-controllers option to the command to get controllers:

dotnet new webapi -n API --use-controllers

Or

dotnet new webapi -n API -controllers
2
On

That command creates a Minimal API by default as shown here. A minimal web API does not have controllers.

Since the default for -controllers is false, entering dotnet new webapi without specifying the option creates a minimal API project.

To create a web API with controllers run the following command instead:

dotnet new webapi -controllers -n API

OR

dotnet new webapi --use-controllers -n API