Create and Run a .NET Core Application Using CLI Tools: .NET Core CLI Part I

DZone 's Guide to

Create and Run a .NET Core Application Using CLI Tools: .NET Core CLI Part I

In this post, we will learn how to create, restore, clean, build, and run .NET Core applications using the latest CLI commands.

· Web Dev Zone ·
Free Resource

.NET Core provides a new cross-platform toolchain for developing .NET applications which are nothing but the .NET CLI tools.

CLI tools are really powerful and easy to use, so, as a developer, we should know how to use these commands.

These commands are very useful if you are looking to create Continuous Integration(CI) and\or Continuous Deployment(CD).

In this post, I will explain how to create, restore, clean, build, and run .NET Core applications using the latest CLI commands.

What Are CLI tools?

As per the Microsoft docs:

The .NET Core command-line interface (CLI) is a new cross-platform toolchain for developing .NET applications. The CLI is a foundation upon which higher-level tools, such as Integrated Development Environments (IDEs), editors, and build orchestrators, can rest.

CLI tools are cross-platform tools and can be used in Windows, Mac, or Linux. Visual Studio internally uses this CLI to restore, build, and publish an application.

.NET CLI tools are generally shipped with the .NET Core installation so if you have installed .NET Core, you can use CLI tools.

If you do not have .NET Core installed, you can download .NET Core from here.

Check Whether You Have the Dotnet CLI Installed

Open the command prompt and type dotnet. If everything is installed correctly, it should give logs as shown below:

Dotnet CLI Syntax

The DotNet CLI commands are really easy to use.

The command syntax generally has 3 parts:

For example, if you wish to create the project in the C# language, you can write below command:

dotnet new console -lang C#
  • dotnetis the driver.
  • newis the command
  • console is the argument
  • -lang is the option

List of Available Commands

Before you start writing the commands, you should have a look at all available commands. You can write a help command to list the details, as shown below:

dotnet help

Create New Project Using CLI

Create a folder under anywhere in the C drive and change the directory to that path in the Command prompt:

cd C:\dev\SampleCliApp

Run the dotnet new command to create a new .NET Core application; we will create an MVC project, so we will run below command:

dotnet new mvc

This will create brand new .NET Core MVC project in the mentioned location:

Note: You can use the command  dotnet new -l to list out all available template options.

Restore Command

Once the application is created, let us run the  restore command which will restore the packages if they are not available on your local machine:

dotnet restore

The restore command internally calls Nuget.exe to restore the tree of dependencies.

This sample application is very small but for a big application, restore plays a big role.

Clean and Build

Once the packages are restored, let us clean and build our project using CLI.

For cleaning, use the  clean command as shown below:

dotnet clean

Important Note: dotnet clean will clear everything under the folder bin\Debug\netcoreapp2.x, it will not clear everything under bin and/or the obj folder. More details here: https://github.com/dotnet/cli/issues/7240

For building the project, use the build command, as shown below:

dotnet build

This will build the project and will put dlls and other build files under bin\Debug\netcoreapp2.x

Run the Application

We can run the application by running the run command, as shown below:

dotnet run

The run command first calls build to make sure the targets are built and then runs the target application.

As you can see in the logs, the application is running on https://localhost:5001

Dotnet Help for Command Details

We have already seen the help command above, but if you need help with specific commands then you can use -h.

For example, if you want to see all arguments/options available for the command build, then write the below command:

dotnet build -h

You can find all details for all commands with -h. 

In a future post, I will explain how to manage Nuget packages along with creating and publishing our own Nuget packages using CLI commands.

We will also use the commands which we used above in a Jenkins pipeline to create the CI.

Hope this helps!

.net core cli, asp.net core, web application development, web dev

Published at DZone with permission of Neel Bhatt , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}