ASP.NET Core Command Line Interface

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 for creating ASP.NET Core Applications, Restoring Packages, Building, Running, and Publishing ASP.NET Core Applications. CLI commands work across different platforms like Windows, macOS, and Linux. The .NET Core CLI command uses Kestrel as the only Web Server that hosts and 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, we can create new .NET Core projects (like console apps, web apps, and libraries). To get started quickly, it comes with templates for different types of applications.
  • Building and Running: we can compile and execute .NET Core applications directly from the command line, which is helpful for testing and development.
  • NuGet Package Management: The CLI commands allow us to manage NuGet packages directly from the command line. We can add, update, or remove packages.
  • Publishing: We can also publish our applications using the CLI command, which includes compiling the application and its dependencies into a set of files or a single executable file.
How to Install Command Line Interface?

When we installed the .NET Core SDK, the .NET Core CLI was also installed by default. So, we aren’t required to install it separately in the development environment, i.e., on our local machine. We can verify whether the .NET CLI is installed using the command prompt. To verify this, open the command prompt (Windows) or terminal (Linux), type dotnet, and press enter, as shown below. If it displays usage and help options, as shown in the image below, the .NET Core CLI is installed properly.

ASP.NET Core Command Line Interface

.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>

Here,

  • dotnet: The base or driver command used to run the CLI.
  • command: The specific command to perform a task (e.g., new, build, run).
  • arguments: The arguments required by the command, such as project or solution name.
  • options: Optional parameters that modify the command’s behavior (e.g., -o for output directory).
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 commands and their uses are given below.

How to Get all .NET Core CLI Commands?

The following are some of the commonly used .NET CLI commands that are relevant to ASP.NET Core development:

  • dotnet new: This command is used to create a new .NET project, configuration file, solution, or template. This command sets up a new project with all the necessary files based on the specified template, such as a console app, web app, or class library.
  • dotnet build: To compile a .NET project and all its dependencies into a set of binaries. The dotnet build command is used to produce executable or library files, i.e., Intermediate Language (IL) code files from source code.
  • dotnet run: This command runs the .NET application directly from the source code. It implicitly calls dotnet build if the project hasn’t been built previously.
  • dotnet publish: This command packs the application and its dependencies into a folder for deployment to a hosting environment.
  • dotnet add package: This command is used to add a NuGet package reference to a project file. This command facilitates the management of external libraries and tools, integrating them into a project directly from the command line.
  • dotnet remove package: To remove a NuGet package reference from a project file. This command is used to manage dependencies by removing unnecessary or unused packages.
  • dotnet restore: To restore the dependencies and tools specified in the project file(s). This is essential for initializing a project or re-syncing the package references. This is usually done automatically when we run dotnet build or dotnet run.
  • dotnet clean: To clean the output of a project. This command clears the build artifacts (such as binaries and obj files) from the output directory, which is useful for ensuring a clean rebuild of the project.
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 Visual Studio. To create a new .NET Core project, we need 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:

Create a New Project using the .NET Core CLI Command

Example to Create a Console Application using .NET Core CLI

The following command creates a new console project with the same name (MyConsoleApp) in the current directory. First, you create a folder called Projects in the D Drive. Inside this Projects folder, you need to create another folder with the name MyConsoleApp. Then, as shown in the image below, you need to set the directory path to D: ProjectsMyConsoleApp.

Example to Create a Console Application using .NET Core CLI

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.

dotnet new console command to create the console application

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.

ASP.NET Core Command Line Interface

If you want to create your project in a specific directory, use the following CLI Command. 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.

ASP.NET Core Command Line Interface

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.

ASP.NET Core Command Line Interface

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

You should get the following output once you type the above command and press enter.

Add Package Reference using .NET Core CLI Command

This will add the Newtonsoft.json package to your project. You can verify this in the project file. So, open the .csproj file in Notepad, and you should get the following.

Add Package Reference using .NET Core CLI Command

Remove Package Reference using .NET Core CLI Command:

The dotnet remove package command removes 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.

Remove Package Reference using .NET Core CLI Command

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:

Restore Packages using .NET Core CLI Command

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:

Build Project using .NET Core CLI Command

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 the output displayed: Hello World!

Run .NET Core Project using .NET Core CLI Command

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.

4 thoughts on “ASP.NET Core Command Line Interface”

Leave a Reply

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