CodeDigest.Com Logo
Featured:

Learn dotnet CLI(Command Line Interface for .Net Core) in 10 Minutes

The dotnet Command Line Interface is a cross platform command line tool used for developing and performing various development activities when developing .Net Core applications.

Project Repository: https://github.com/dotnet/cli

 

Note – Originally when Asp.Net Core 1.0 was released there was a similar tool set called DNX (Dot Net Execution) runtime to execute .Net Core code and DNU (Dotnet Developer Utillity) for building and publishing .Net Core application. These toolsets are retired after the release of dotnet CLI as all the functionality are now part of dotnet CLI. There is another DNVM (Dotnet Version Manager) tool which is nothing but script used to install these toolsets.

 

To get this toolset, you need to install the .Net Core SDK from here. Please note installing only runtime will not bring this tooling support into your development machine. Dotnet CLI toolset is capable of installing side by side and so, multiple versions of toolset can be installed on your machine.

dotnet CLI Syntax

A typical command follows the below syntax,

 

dotnet [verb] [arguments]

 

The dotnet part of the above syntax is the driver which runs the command and runs the .Net Core application.

Let’s see some of the important commands and activities we can do with dotnet CLI tools.

Using dotnet CLI

Go to Command prompt, and type dotnet and enter. This will emit the below information into command window. This means that you have a dotnet CLI installed on your machine.

C:\Learn\Asp.Net Core\CLI>dotnet

 

Microsoft .NET Core Shared Framework Host

  Version  : 1.1.0

  Build    : 928f77c4bc3f49d892459992fb6e1d5542cb5e86

Usage: dotnet [common-options] [[options] path-to-application]

 

Common Options:

  --help                           Display .NET Core Shared Framework Host help.

  --version                        Display .NET Core Shared Framework Host version.

 

Options:

  --fx-version <version>           Version of the installed Shared Framework to

use to run the application.

  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.

 

Path to Application:

  The path to a .NET Core managed application, dll or exe file to execute.

If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.

 

To get started on developing applications for .NET Core, install the SDK from:

  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

 

C:\Learn\Asp.Net Core\CLI>

 

Commands

Type dotnet –help will list all the commands the tool is offering. All the commands listed below

Command

Description

new

Initialize .NET projects.

restore

Restore dependencies specified in the .NET project.

build

Builds a .NET project.

publish

Publishes a .NET project for deployment (including the runtime).

run

Compiles and immediately executes a .NET project.

test

Runs unit tests using the test runner specified in the project.

pack

Creates a NuGet package.

migrate

Migrates a project.json based project to a msbuild based project

clean

Clean build output(s).

sln

Modify solution (SLN) files.

Project modification commands

add

Add items to the project

remove

Remove items from the project

list

List items in the project

Advanced Commands

nuget

Provides additional NuGet commands.

msbuild

Runs Microsoft Build Engine (MSBuild).

vstest

Runs Microsoft Test Execution Command Line Tool.

 

Creating New Application:

  1. To create new MVC application

 

dotnet new MVC

 

This will create new MVC application in the current directory.

Use “console” as argument instead of MVC to create a console application and “library” to create a class library project.

  1. To restore all Nuget package, the command is,

 

dotnet restore

 

This will restore all the required packages as seen above.

  1. To build application,

 

dotnet build

 

  1. To run application,

 

dotnet run

 

This will run the application and start the Kestrel webserver which will listen to port 5000 for incoming request. Visiting http://localhost:5000 will bring app like below.

Alternatively, you can also run the app by using the below command.

 

dotnet appdll.dll