ASP.NET Core Command Line Interface (.NET Core CLI)
In this article, I will 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 discussing 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 ASP.NET Core Applications, Restoring Packages, Building, Running, and Publishing ASP.NET Core Applications. The .NET Core CLI command uses Out of Process hosting for any web application, i.e., the Kestrel server runs 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 like Visual Studio Code use these CLI Commands to support creating, restoring, publishing, and running .NET Core applications.
Key Features of .NET Core CLI:
- Project Creation: Using simple commands, you can create new .NET Core projects (like console apps, web apps, and libraries). It comes with templates for different types of applications to get you started quickly.
- Building and Running: You can compile and execute your .NET Core applications directly from the command line. This is helpful for testing and development purposes.
- NuGet Package Management: The CLI allows you to manage NuGet packages (libraries and tools that are shared across projects) directly from the command line. You can add, update, or remove packages.
- Testing: It supports running unit tests for your application from the command line, making it easy to integrate into continuous integration pipelines.
- Publishing: You can publish your applications (for deployment to a server or hosting environment) using the CLI, which includes compiling the application and its dependencies into a set of files or a single executable.
- Cross-Platform: One of the most significant advantages of the .NET Core CLI is its cross-platform capability, allowing developers to use the same toolchain on different operating systems.
- Integration with Visual Studio and other IDEs: While it is a command-line tool, it integrates seamlessly with Visual Studio, Visual Studio Code, and other popular Integrated Development Environments (IDEs), providing a consistent development experience.
- Extensibility: The CLI can be extended with custom tools and commands to suit the specific needs of a development team or project.
How to Install Command Line Interface?
When we installed .NET Core SDK, then by default, the .NET Core CLI was also installed. So, we aren’t required to install it separately in 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), type dotnet, and press enter as shown below. If it displays usage and help 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.
Here are some of the commonly used .NET CLI commands that are relevant to ASP.NET Core development:
- dotnet new: This command creates a new project, configuration file, or solution based on the specified template. For ASP.NET Core, you can create projects like web apps, API apps, MVC apps, etc. For example, dotnet new mvc creates a new ASP.NET Core MVC project.
- dotnet build: This compiles the project and its dependencies into a set of binaries. The binaries include the project’s code in Intermediate Language (IL) files with a .dll extension.
- dotnet run: This command runs the application from the source code. It implicitly calls dotnet build if the project hasn’t been built previously.
- dotnet publish: This packs the application and its dependencies into a folder for deployment to a hosting system.
- dotnet add package: This is used to add a NuGet package to a project. For example, you might use this to add additional ASP.NET Core middleware or dependencies.
- dotnet restore: This command restores the dependencies and tools of a project. This is usually done automatically when you run dotnet build or dotnet run.
- dotnet ef: If you’re using Entity Framework Core, this set of commands is crucial for database migrations and updates.
- dotnet test: This runs unit tests using a test runner specified in the project file.
- dotnet user-secrets: This is a tool for managing application secrets during development.
- dotnet watch: This command is used for continuous development. It watches the source files, and when it sees changes, it automatically rebuilds the project and can even refresh the browser.
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 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 image below:
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.
You can also give your project a different name if you want. 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 named 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 named 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 image below, which will remove the Newtonsoft.json package from your project. 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:
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!
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.
In the next article, I will discuss Project Templates in ASP.NET Core Applications. In this article, I explain how to develop, build, and run a .NET Core Application using the .NET Core CLI (Command Line Interface) Command. I hope you enjoy this article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.