Exploring .NET Core CLI and New Project Templates
Command Line Interface (CLI) is a very popular tool is almost all popular frameworks like Yeoman Generator, Angular, and others. It lends developers access to execute commands to create, build, and run projects, restore packages, and so on.
.NET CLI provides a toolset with a handful commands that can be executed from the command line interface to create .NET Core projects, restore dependencies, and build and run projects. Under the wire, Visual Studio 2015/2017 and Visual Studio Code even uses this tool to perform different options taken by the developers from their IDE; for example, to create a new project using .NET CLI, we can run the following command:
dotnet new
It will list down the available templates and the short name that can be used while creating the project.
Here is the screenshot containing the list of project templates that can be used to create/scaffold projects using .NET Core CLI:
And by running the following command, a new ASP.NET Core MVC application will be created:
dotnet new mvc
The following screenshot shows the provisioning of the new MVC project after running the preceding command. It creates the project in the same directory where the command is running and restores all the dependencies:
To install the .NET Core CLI toolset, there are some native installers available for Windows, Linux, and macOS. These installers can install and set up the .NET CLI tooling on your machine and developers can run the commands from the CLI.
Here is the list of commands with their descriptions that are provided in the .NET Core CLI:
Command | Description | Example |
| Creates a new project based on the template selected |
|
| Restores all the dependencies defined in the project |
|
| Builds the project |
|
| Runs the source code without any additional compile |
|
| Packages the application files into a folder for deployment |
|
| Used to execute unit tests |
|
| Executes unit tests from specified files |
|
| Packs the code into a NuGet package |
|
| Migrates .NET Core preview 2 to .NET Core 1.0 |
|
| Cleans the output of the project |
|
| Modifies a .NET Core solution |
|
| Displays the list of commands available to execute through .NET CLI |
|
| Stores the specified assemblies in the runtime package store |
|
Here are some of the project level commands that can be used to add a new NuGet package, remove an existing one, list references, and others:
Command | Description | Example |
| Adds a package reference to the project |
|
| Removes a package reference from the project |
|
| Adds a project reference to the project |
|
| Removes the project reference from the project |
|
| List down all the project references in the project |
|
The following are some common Entity Framework Core commands that can be used to add migration, remove migration, update the database, and so on.
Command | Description | Example |
| Adds a new migration |
- |
| List available migrations |
|
| Remove specific migration |
- |
| To update the database to a specified migration |
- |
| Drops the database |
|
Here are some of the server level commands that can be used to delete the NuGet package from its actual source repository from the machine, add NuGet package into its actual source repository on the machine, and so on:
Command | Description | Example |
| Deletes the package from the server |
|
| Pushes a package to the server and publishes it |
|
| Lists the local NuGet resources |
|
| Builds a project and all of its dependencies |
|
| The script to install the .NET CLI tools and the shared runtime |
|
To run the preceding commands, we can use the tool known as dotnet from the command line and specify the actual command followed by that. When the .NET Core CLI is installed, it is set into the PATH variable in Windows OS and can be accessed from any folder. So, for example, if you are at your project root folder and wanted to restore the dependencies, you can just call the following command and it will restore all the dependencies that have been defined in your project file:
dotnet restore
The preceding command will start restoring the dependencies or project-specific tools, as defined in the project file. The restoration of tools and dependencies are done in parallel:
We can also set the path where packages can be restored by using the --packages
argument. However, if this is not specified, it uses the .nuget/packages
folder under the system's user folder. For example, the default NuGet folder for Windows OS is {systemdrive}:\Users\{user}\.nuget\packages
and /home/{user}
for Linux OS, respectively.