ASP.NET Core Command Line Interface (.NET Core CLI)
In this article, I am going to discuss how to create, build, and run ASP.NET Core Applications using ASP.NET Core CLI (Command Line Interface) Commands. Please read our previous article, where we discussed Developer Exception Page Middleware in ASP.NET Core Applications.
ASP.NET Core Command Line Interface:
The .NET Core CLI (Command Line Interface) is a new cross-platform tool that is used for creating, restoring packages, building, running, and publishing ASP.NET Core Applications. The .NET Core CLI command uses Out of Process hosting for any kind of web application, i.e., it uses the Kestrel server to run the application.
As of now, all the applications we created are using Visual Studio. Visual Studio internally uses the .NET CLI commands to restore, build, and publish the applications. Other higher-level IDEs, editors, and tools, such as Visual Studio Code, use these CLI Commands to support creating, restoring, publishing, and running .NET Core applications.
When we installed .NET Core SDK, then by default, the .NET Core CLI was also installed. So, we don’t require installing it separately on the development environment, i.e., on our local machine. We can verify the same, i.e., whether the .NET CLI is installed or not using the command prompt.
To verify the same, open the command prompt (Windows) terminal (Linux) and, type dotnet, and press enter as shown below. If it displays usage and helps options, as shown in the below image, it means the .NET Core CLI is installed properly.
.NET Core CLI Command Structure:
The .NET Core CLI command structure is nothing but how we write the .NET Core CLI command. The following is the command structure of .NET Core CLI Command:
dotnet <command> <argument> <option>
Note: All the .NET Core CLI commands start with the driver named dotnet. The driver, i.e., dotnet, starts the execution of the specified command. After dotnet, we need to specify the command (also known as the verb) to perform a specific action. Each command can be followed by arguments and options.
How to Get all .NET Core CLI Commands?
Open the command prompt, type dotnet help, and press the enter button to display all the .NET Core CLI commands. Some of the command and their use is given below.
Create a New Project using the .NET Core CLI Command:
Let’s create, restore, build, and run the .NET Core console application using the command-line interface without using Visual Studio. To create a new .NET Core project, we have to use the ‘new’ command followed by the template name argument. We can create the console, class library, web, web app, MVC, Web API, razor, angular, react, etc. Project using CLI.
The following command creates a new dotnet core project using the TEMPLATE:
dotnet new <TEMPLATE>
You can find out the list of templates using the following CLI Command:
dotnet new list
Once you type dotnet new list and press enter, it will show you the list of available templates based on the .NET Core Version installed on your machine, as shown in the below image:
Example to Create a Console Application using .NET Core CLI
The following command creates a new console project in the current directory with the same name as the current directory. First of all, you need to create a folder called Projects in the D Drive, and then inside this Projects folder, you need to create another folder with the name MyConsoleApp, and you need to set the directory path the D:\Projects\MyConsoleApp as shown in the below image.
Then you need to use the dotnet new console command to create the console application. Once you execute the dotnet new console command, it will create a new console application and get the following output. Here, the project is created with the name MyConsoleApp.
If you want, then you can also give a different name to your project. For example, the following command will create a new console project named MyConsoleApp1. The -n or –name option specifies the name of the project.
dotnet new console -n MyConsoleApp1
Once you execute the above command, it will create a new console application with the name MyConsoleApp1, and you will get the following output.
If you want to create your project in a specific directory, use the following CLI Command as an example. This command will create a new console application called MyConsoleApp2 in the D:\\ MyProjects directory. The -o or —output option lets you specify the output directory for the project. To execute this command, make sure you first create a folder named MyProjects in your D drive.
dotnet new console -n MyConsoleApp2 -o D:\\MyProjects
Once you execute the above command, it will create a new console application with the name MyConsoleApp2 with the D:\\ MyProjects directory, and you will get the following output.
After creating the project, navigate to the project directory (folder) in the command prompt to apply project-specific commands. As we created the project in D:\\ MyProjects directory folder, then went to the D:\\ MyProjects directory in the command prompt, as shown below.
Add Package Reference using .NET Core CLI Command:
We often need to add NuGet package references for different purposes. For example, apply the following command to add the Newtonsoft.json package to your console project.
dotnet add package Newtonsoft.json
Once you type the above command and press enter then, you should get the following output.
This will add the Newtonsoft.json package to your project. You can verify the same in the project file. So, open the .csproj file in Notepad, and you should get the following.
Remove Package Reference using .NET Core CLI Command:
The “dotnet remove package” command provides a convenient option to remove a NuGet package reference from a project. If you want to remove the NuGet Package that we just installed Newtonsoft.json, then you need to execute the below command,
dotnet remove package Newtonsoft.json
So, in the command prompt, type “dotnet remove package Newtonsoft.json” and press the enter button as shown in the below image, which will remove the Newtonsoft.json package from your project, and you verify the same in the project file.
Restore Packages using .NET Core CLI Command:
To restore packages or to update existing packages in your project, you can use the “dotnet restore” command as below:
Build Project using .NET Core CLI Command:
In order to build a new or existing project, we need to use the “dotnet build” command as shown below, which will build your .NET Core Project:
Run .NET Core Project using .NET Core CLI Command:
To run the .NET Core project, we need to use the “dotnet run” command, as shown below. Here, you can see it display the output Hello World!
Key Features of ASP.NET Core CLI Command:
The ASP.NET Core Command Line Interface (CLI), commonly referred to as “dotnet CLI,” is a set of command-line tools provided by Microsoft to help developers create, build, run, and manage ASP.NET Core applications from the command prompt or terminal. The CLI offers a powerful and flexible way to work with ASP.NET Core projects and perform various development tasks without relying solely on integrated development environments (IDEs) like Visual Studio.
Here are some of the key functionalities and tasks that the ASP.NET Core CLI supports:
- Creating Projects: Using templates, you can use the CLI to create new ASP.NET Core projects. For example, you can create a new web application, a web API, a class library, and more.
- Building Projects: The CLI allows you to build your projects using the dotnet build command. This compiles your code, resolves dependencies, and produces the output binaries.
- Running Applications: You can execute your ASP.NET Core applications using the dotnet run command. This starts your application and hosts it on a local development server (usually Kestrel).
- Publishing Applications: The dotnet publish command packages your application for deployment. It compiles the code, includes all required dependencies, and prepares the output for deployment to a server.
- Adding Packages: The CLI supports adding NuGet packages to your project using the dotnet add package command.
- Managing Dependencies: Using the CLI, you can manage package references, remove packages, and update package versions.
- Database Migrations: For projects that use Entity Framework Core, the CLI can be used to manage database migrations and apply changes to the database schema.
- Global Tools: The CLI allows you to install and manage global tools that provide additional functionality, like code analysis or project scaffolding.
- Project Scaffolding: The CLI can generate code files and project structures based on templates.
- Testing: You can run unit tests using the dotnet test command.
- Code Generation: The CLI supports code generation for various parts of your application, such as controllers, views, and more.
The ASP.NET Core CLI is designed to work across different platforms (Windows, Linux, macOS) and integrates well with continuous integration (CI) and continuous deployment (CD) workflows. It provides a consistent and efficient way to manage your ASP.NET Core projects, regardless of your preferred development environment.
Overall, the ASP.NET Core CLI is a valuable tool for developers working with ASP.NET Core projects, offering increased flexibility and control over the development process.
In the next article, I am going to discuss Project Templates in ASP.NET Core Applications. I explain how to develop, build, and run .NET Core Application using .NET Core CLI (Command Line Interface) Command in this article. I hope you enjoy this article.