DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Deploy an Application for Azure Container Registry
  • Test-Driven Development With The oclif Testing Library: Part One
  • AppOps with Kubernetes and Devtron - The Perfect Fit
  • Automate and Persist MuleSoft CloudHub Application Logs

Trending

  • How to Submit a Post to DZone
  • The Evolution of Software Integration: How MCP Is Reshaping AI Development Beyond Traditional APIs
  • Understanding k-NN Search in Elasticsearch
  • 12 Principles for Better Software Engineering
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Create and Run a .NET Core Application Using CLI Tools: .NET Core CLI Part I

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.

By 
Neel Bhatt user avatar
Neel Bhatt
·
Jun. 25, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
23.7K Views

Join the DZone community and get the full member experience.

Join For Free

.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#
Here:
  • 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!

Command-line interface .NET application Command (computing) Continuous Integration/Deployment Cross platform

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

Opinions expressed by DZone contributors are their own.

Related

  • Deploy an Application for Azure Container Registry
  • Test-Driven Development With The oclif Testing Library: Part One
  • AppOps with Kubernetes and Devtron - The Perfect Fit
  • Automate and Persist MuleSoft CloudHub Application Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: