Creating a new minimal API project
Let’s start with our first project and try to analyze the new template for the minimal API approach when writing a RESTful API.
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
Follow these steps to create a new project in 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
API
in 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
For this example we will use the name Chapter01
, but you can choose any name that appeals to you.
- 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
- Click Create and, after a few seconds, you will see the code of your new minimal API project.
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
Creating a project with Visual Studio Code is easier and faster than with Visual Studio 2022 because you don’t have to use a UI or wizard, rather just a terminal and the .NET CLI.
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.