Creating a new minimal API project
In this section, we will create our first minimal API project. We will start by using Visual Studio 2022 and then we will show how you can also create the project with Visual Studio Code and the .NET CLI.
Creating the project with Visual Studio 2022
- Open Visual Studio 2022 and on the main screen, click on Create a new project:
Figure 1.1 – Visual Studio 2022 splash screen
- On the next screen, write
APIin the textbox at the top of the window and select the template called ASP.NET Core Web API:
Figure 1.2 – Create a new project screen
- Next, on the Configure your new project screen, insert a name for the new project and select the root folder for your new solution:
Figure 1.3 – Configure your new project screen
- On the following Additional information screen, make sure to select .NET 6.0 (Long-term-support) from the Framework dropdown. And most important of all, uncheck the Use controllers (uncheck to use minimal APIs) option.
Figure 1.4 – Additional information screen
Now we are going to show how to create the same project using Visual Studio Code and the .NET CLI.
Creating the project with Visual Studio Code
You don’t need to install anything new for this because the .NET CLI is included with the .NET 6 installation (as in the previous versions of the .NET SDKs). Follow these steps to create a project using Visual Studio Code:
- Open your console, shell, or Bash terminal, and switch to your working directory.
- Use the following command to create a new Web API application:
dotnet new webapi -minimal -o Chapter01
As you can see, we have inserted the
-minimal parameter in the preceding command to use the minimal API project template instead of the ASP.NET Core template with the controllers.
- Now open the new project with Visual Studio Code using the following commands:
cd Chapter01 code.
Now that we know how to create a new minimal API project, we are going to have a quick look at the structure of this new template.