.NET Core Command-line Interface
The .NET Core command-line interface (CLI) is a new cross-platform tool for creating, restoring packages, building, running and publishing .NET applications.
We can verify whether the CLI is installed properly by opening command prompt in Windows and writing dotnet and pressing Enter. If it displays usage and help as shown below then it means it is installed properly.

.NET Core Command-line Interface
Command Structure:
The following is a command structure.
dotnet <command> <argument> <option>
Create a New Project:
To create a new .NET Core project, we have to use new command followed by template name argument.
dotnet new console
The following creates new console project in the current directory with the same name as current directory.
dotnet new console
The following command creates a new console project named MyConsoleApp. The -n or --name option species the name of a project.
dotnet new console -n MyConsoleApp
The following command creates a new console application named MyConsoleApp to MyProjects directory. The -o or --output option is used to specify an output directory where the project should be generated.
dotnet new console -n MyConsoleApp -o C:\MyProjects
After creating a project, navigate to the project directories in command prompt to apply project specific commands which is C:\MyConsoleApp in our case.
Add Package Reference:
We often need to add NuGet package reference for different purposes. For example, apply the following command to add Newtonsoft.json package to our console project.
C:\MyConsoleApp>dotnet add package Newtonsoft.json
This will add Newtonsoft.json package to our project. We can verify it by opening .csproj file.
Restore Packages:
To restore packages or to update existing packages, we can use restore command as below.
C:\MyConsoleApp>dotnet restore
Build Project:
To build a new or existing project, apply
C:\MyConsoleApp>dotnet build
command.Run project:
To run our console project, apply
dotnet run
command as shown below.
As you can see above, it displays an output "Hello World!".
Getting Help:
We can get help on any .NET Core CLI commands by typing -h or -help at the end of the command we want to get help on.

No comments:
Post a Comment