.NET Core makes it easy to get started with creating any type of application, whether it be an MVC app, Web API, or desktop software. To achieve this, .NET provides a command-line tool that is handy if a developer does not want to use an integrated development environment (IDE), such as Visual Studio, for creating their applications.
Coding, building, and running projects using the .NET CLI (Command-line Interface) is quite easy and fast. To get started using the command-line tool, you need to download the .NET Core SDK, which you can get from here: https://www.microsoft.com/net/download/core.
To check if you already have installed .NET Core on your system, open the command prompt and type dotnet into it. If everything is installed correctly, then you should see the following log screen:
Commands in dotnet CLI
The structure of any command in the dotnet CLI is as follows:
For example, if you are going to create a project in C#, then you need to use the following command:
dotnet new console -lang C#
In the above C# code, the first keyword denotes the driver named dotnet, new is a keyword that denotes the command to perform a specific action, console, denotes the argument, and lang denotes the option.
Below, you can find some of the frequently used commands on .NET CLI:
- new: Creates a new project or solution
- restore: Restores the dependencies of a project.
- build: Builds a project and its dependencies.
- run: Runs source code without any explicit compilation
- publish: Packs the application and its dependencies into a single folder for deployment.
- add package: Adds a package reference to a project.
- add reference: Adds project references.
- remove package: Removes package reference from the project.
- remove reference: Removes project reference
- nuget delete: Deletes a package from the server.
- nuget locals: Lists local NuGet resources.
- nuget push: Pushes a package to the server and publishes it.
In the following section, we will come across some of the important commands that can be used during different phases of building an application from creation to deployment.
How to Create a New Project using Command–line Interface
Creating a new project in .NET is a straightforward process. You can create a new console app, Web API, MVC application, and so forth by making use of the CLI tool.
For instance, to create a console application with the same name as the name of the directory in which it is being generated, you need to use the following CLI command:
dotnet new console
To create a new project with a specific name, use the –n or –name option:
dotnet new console –n MyConsoleApp
To create a new console app in a specific directory with a specific name, use the following command:
dotnet new console –n MyConsoleApp –o D:\Projects\MyConsoleApp
Here, you have to use the –o option, that indicates an output directory. It will create the project in the desired directory as shown below:
Adding a Package with .NET CLI
We often need to create different packages for different needs. For instance, to add a Newtonsoft.json package to our application, use the following command:
D:\Projects\MyConsoleApp>dotnet add package Newtonsoft.json
Restoring Packages with .NET CLI
To restore or update existing packages in your project, use the following command:
Build a Project with .NET CLI
Build an existing project using the following command:
Run a Project with .NET CLI
To run our .Net Core project, use the following command:
This results in:
How to Get Help on the .NET Command-line Interface
If you are not sure which command to use for carrying out a specific operation, you can get help right from the .NET CLI too. The tool assists developers by allowing them to type –h or –help at the end of the command. For instance, type the following command for getting help on the new command:
The –help command helps programmer to know how to use that command and what are the arguments and options we can use with it are.
It is recommended for novice programmers that, before writing any command on the command-line interface, it is always a good practice to check the command first with -help. Doing so will show a list of details for that particular command.
Developers can use the command-line interface tool to perform different tasks for distinct phases of application development, from creating an application to publishing and deploying it.