Creating ASP.NET Core Web API Project using .NET Core CLI

Creating ASP.NET Core Web API Project using .NET Core CLI

In this article, I will discuss How to Create an ASP.NET Core Web API Project using .NET Core CLI. We will also discuss how to build and run that project. Finally, we will see how to test the API, which is by default provided by that project using Swagger, Browser, and Postman. Please read our previous article discussing the Environment Setup for ASP.NET Core Web API Application development.

Creating ASP.NET Core Web API Project

In .NET Core, there are two ways to create a project. They are as follows:

  • Using .NET Core CLI (Command Line Interface)
  • Using Visual Studio
ASP.NET Core Web API Project using .NET Core CLI

First, we need to select the folder location where you need to create the ASP.NET Core Web API Project. Suppose we want to create the ASP.NET Core Web API Project in the D:\CLI folder. First, create a folder with the name CLI within the D Drive of your machine. Then open the command prompt in Administrator mode and change the directory location to D drive in the command prompt by typing D: and then press the enter key as shown below.

ASP.NET Core Web API project using .NET Core CLI

Once you type D: and press the Enter button, then it will change the directory path to the D drive. Then, decide inside which folder of D Drive you want to create the project. I am going to create the project inside the CLI folder. So, change the directory location to the CLI folder by typing CD CLI and pressing the Enter key, as shown below.

Creating ASP.NET Core Web API Project

Once you type CD CLI and press the enter key, it will change the directory path to the D:\CLI folder, as shown in the below image.

Creating ASP.NET Core Web API Project

Let’s see the command to create a new ASP.NET Core Project using .NET Core CLI. First, type dotnet new list and press the enter button as shown in the below image.

ASP.NET Core Web API Project using .NET Core CLI

Once you type dotnet new list and press the enter button, it will display the following. As you can see in the below image, it shows different templates, the short name for the template, and the default language for the template.

ASP.NET Core Web API Project using .NET Core CLI

As you can see in the above image, it provides one template called ASP.NET Core Web API, and to create that project, we can use the command webapi (Short Name of the template). The default programming language for this Web API Project is C#, and apart from C# language, you can also use F# language.

Let us create the ASP.NET Core Web API project. You can create a web API project using two ways. They are as follows:

  • dotnet new webapi: If you only type dotnet new webapi, then a new project will be created inside the CLI folder with the default name.
  • dotnet new webapi –name MyFirstWebAPIProject: With this command, the .NET Core CLI will create a project inside the CLI folder with the name MyFirstWebAPIProject.

So, let us use the second option to create the ASP.NET Core Web API Project. Type dotnet new webapi –name MyFirstWebAPIProject command and press the enter button in the command prompt as shown in the below image.

How to Create ASP.NET Core Web API project using .NET Core CLI

Once you type dotnet new webapi –name MyFirstWebAPIProject and press enter, you will get the below output saying that the template ASP.NET Core Web API was created successfully.

Creating ASP.NET Core Web API Project using .NET Core CLI

Now, inside the CLI Folder, you can see a folder with your project name (in my case, the folder name is MyFirstWebAPIProject). Inside that folder, you can see the following default project files and folders provided by the ASP.NET Core Web API template, as shown in the image below.

Creating ASP.NET Core Web API Project using .NET Core CLI

Opening ASP.NET Core Web API Project in Visual Studio Code:

First of all, you should have installed Visual Studio Code on your machine. Let us see how to open the above project in Visual Studio Code. First, change the directory in the command prompt to the project directory (CD MyFirstWebAPIProject), as shown below.

Opening ASP.NET Core Web API project in Visual Studio Code

The directory will change once you type CD MyFirstWebAPIProject and press enter. Then, to open the project files and folders in Visual Studio Code, type “code .” (code space dot) and press the enter button as shown below.

Opening ASP.NET Core Web API project in Visual Studio Code

Once you type “code .” (code space dot) and press the enter button, it will open the ASP.NET Core Project in Visual Studio code, as shown in the image below.

Opening ASP.NET Core Web API Project in Visual Studio Code

In our upcoming sessions, we will discuss each of the above files and folders in detail.

How do you build an ASP.NET Core Web API Project using .NET Core CLI?

Let us see how to build the above-created ASP.NET Core Web API Project using .NET Core CLI. We can build the .NET Core Project in two ways.

  • Using Visual Studio Code Terminal
  • Using Command Prompt
Build ASP.NET Core Web API Project Using Visual Studio Code Terminal

First, open a new Terminal in the Visual Studio Code. To do so, click on the Terminal Menu and select the New Terminal option from the Visual Studio Code menus, as shown in the image below.

Build ASP.NET Core Web API Project Using Visual Studio Code Terminal

Once you click on the New Terminal option, it will open the terminal window, as shown in the below image. In the terminal, type dotnet build and press the Enter button, as shown in the below image. This command is used to build an existing project. Once you type dotnet build and press the enter button, it will build the project, and you will get the following message.

Build ASP.NET Core Web API Project Using Visual Studio Code Terminal

As you can see in the above image, the build is successful, with no error and no warning.

Build ASP.NET Core Web API Project Using Command Prompt:

In the command prompt, first set the directory to your project folder, then type dotnet build and press the enter button, as shown in the image below.

Build ASP.Net Core Web API Project Using Command Prompt

Once you type dotnet build and press the enter button, it will build your project, and you will get the message below.

Build ASP.NET Core Web API Project Using Command Prompt

How to Run the ASP.NET Core Web API Project using Visual Studio Code?

Now, let us see how to run the above ASP.NET Core Web API Project using Visual Studio Code. The .NET Core CLI provides the run command to run the ASP.NET Core Web API Application. So, in the terminal, type dotnet run and press the enter button, as shown in the image below.

How to run the ASP.NET Core Web API project using .NET Core CLI?

Once you type dotnet run and press the enter button, you will get the below message. 

How to run the ASP.NET Core Web API project using .NET Core CLI?

How to Run ASP.NET Core Web API Project using Command Prompt?

In the command prompt, first set the directory to your project folder, then type the dotnet run command and press the enter button, as shown in the image below. But before executing the following command please close the Visual Studio Terminal where we have already started running the application, otherwise, you will get some error.

How to run ASP.NET Core Web API Project using Command Prompt?

Once you type dotnet run and press the enter button, it will run your project, and you will get the message below.

How to Run ASP.NET Core Web API Project using Command Prompt?

As you can see in the above image, our ASP.NET Core Web API Application is running on the following port.

http://localhost:5008

Now, open the above URL in any of your browsers, and you will get a 404 error.

How to Run ASP.NET Core Web API Project using Command Prompt?

Don’t worry. Just type swagger at the end of the URL and press enter, and you will get the following webpage.

How to Run ASP.NET Core Web API Project using Command Prompt?

The swagger will display the details of all the Web APIs available in your project. As you can see in the above image, it shows one API, i.e., /WeatherForecast, and the type is Get. Now click on the /WeatherForecast API to see the details shown in the image below.

How to run the ASP.NET Core Web API project using .NET Core CLI?

Once you click on the /WeatherForecast API, it will show you the API details, as shown in the image below.

How to run the ASP.NET Core Web API project using .NET Core CLI?

Note: Swagger is also a client API Tool, and using Swagger, we can also test the Web APIs. If you are using the default ASP.NET Core Web API project, then by default, swagger is installed into the project.

Test ASP.NET Core Web API using Swagger:

Now let us see how to test the API, i.e., WeatherForecast API, using swagger. To test the API using Swagger, click on the try it out button, as shown in the image below.

Test ASP.NET Core Web API using Swagger

Once you click on the Try it Out button, it will open below, and again here, click on the Execute button as shown in the below image.

Test ASP.NET Core Web API using Swagger

Once you click on the Execute button, it will give you the response, as shown in the image below. Here, you can find the request URL, the response body, the response status code, and the response headers.

Test ASP.NET Core Web API using Swagger

In our upcoming session, we will discuss where this response is coming from. Even if you want, you can directly call the Request URL in any of your browsers, and you will get the response as expected, as shown in the below image.

How to Create, Build, Run, and Test an ASP.NET Core Web API Project using .NET Core CLI

In the next article, I will discuss How to Test ASP.NET Core Web API using Postman. Here, in this article, I try to explain How to Create, Build, Run, and Test an ASP.NET Core Web API Project using .NET Core CLI, and I hope you enjoy this Create, Build, Run, and Test ASP.NET Core Web API project using .NET Core CLI article.

Leave a Reply

Your email address will not be published. Required fields are marked *