Creating a simple REST web API project
In this section, we will use the .NET command-line interface (.NET CLI) to create a basic web API project and see how it works.
The .NET CLI is a command-line tool that helps you to create, develop, build, run, and publish .NET applications. It is included in the .NET SDK.
You have multiple ways to run .NET CLI commands. The most common way is to run the command in the terminal window or command prompt. Also, you can run the command in VS Code directly. VS Code provides an integrated terminal that starts at the root of your workspace. To open the terminal in VS Code, you can do any one of the following:
- Press Ctrl + ` (on Windows) or Command + ` (on macOS) to open the terminal
- Use the View | Terminal menu item to open the terminal
- From the Command Palette, use the View: Toggle Terminal command to open the terminal
In the terminal, navigate to a folder where you want to create the project, then create a web API project by running the following command:
dotnet new webapi -n MyFirstApi -controllers cd MyFirstApi code .
The preceding commands create a new web API project and open it in VS Code. dotnet new
provides many options to create various types of projects, such as web APIs, console apps, class libraries, and so on.
There are some options we can use to specify the project:
-n|--name <OUTPUT_NAME>
: The name for the created output. If not specified, the name of the current directory is used.-o|--output <OUTPUT_PATH>
: The output path for the created project. If not specified, the current directory is used.-controllers|--use-controllers
: Indicates whether to use controllers for actions. If not specified, the default value isfalse
.-minimal|--use-minimal-apis
: Indicates whether to use minimal APIs. The default value isfalse
, but the-controllers
option will override the-minimal
option. If neither-controllers
nor-minimal
is specified, the default value of the-controllers
option, which isfalse
, will be used, so a minimal API will be created.
Important note
Since .NET 6.0, ASP.NET Core 6.0 provides a new way to create web API projects, which is called minimal APIs. It is a simplified approach for building APIs without controllers. We will introduce minimal APIs later. For now, we will use the traditional way to create a web API project with controllers. So, we need to specify the --
use-controllers
option.
To learn more about the dotnet new
command, check this page: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new. We will introduce more details on the dotnet
command in the following sections.
When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:
The reason is that VS 2022 will create a sln file for the project, but .NET CLI does not. When using VS Code to open the project, the C# DevKit will create the sln file. I think it's worth mentioning it here.
The C# Dev Kit extension provides a new feature, the solution explorer, which is located at the bottom. This feature is especially useful when working with multiple projects in one solution. You can drag and drop the SOLUTION EXPLORER to the top to make it more visible.
When you use VS Code to open the project, the C# Dev Kit extension can create a solution file for you. This feature makes VS Code more friendly to C# developers. You can see the following structure in the Explorer view:
Figure 2.2 – The solution explorer and the folder structure
Next, we can start to build and run the project.