In this article, we will talk about how to get started with .Net Core Command Line Tools. So let`s get started.
It is a general purpose cross platform development platform supported by Microsoft.
In this article, we will talk about the command line tooling provided by .Net Core.
.Net Core 1.0 command line tool chain is a cross platform tool which allows us to develop .Net core applications using command line interface. It`s first RTM release was announced on June 27, 2016. It is the primary layer on which other higher level tools are built. We can use the .Net core command line interface on Windows, Mac and various flavors of Linux including RedHat. For more information on supported opearating system click here. Commands are same on each supported platform. Means even if I use Mac machine to learn the command line tooling, I can use the same commands on Windows or Linux machine and continue learning.
It`s very easy to install .Net Core Command Line Tooling. Just open up the browser and enter http://dot.net/core URL. It will take us to the page where we can download the installer according to the operating system and install .Net Core Command Line Tooling. I am using the windows machine but you can follow along and execute these commands on any supported operating system.
After installation, to verify that it`s installed properly just open up the command prompt (Shortcut: WIN+R and type cmd) and type dotnet and hit enter. You will be presented with following information (F: 1).
To get the information about the current version of the command line tooling, type the following command and hit enter.
Output of above command is as shown below (F: 2):
As we can see in above image, the current installed version is 1.0.0-preview2-003131. Multiple versions of the command line tooling can be installed side by side. Location for the installed versions on windows machine is C:\Program Files\dotnet\sdk (F:3).
As we can see in above image, I have two versions installed on my machine. Latest version which was also displayed when we run the dotnet --version command (Refer F: 2) is highlighted in red color in above image.
Now let`s run following command in command prompt to get the list of command that we can execute.
Output of above command is as shown below (F: 4).
As we can see in above image there are total 7 commands which are listed at the bottom of the image along with their description that we can execute. In this article, we will focus three main commands:
The first command is new command using which we can create a basic .Net project. To get more information about the new command, type the following command in command prompt and hit enter.
dotnet new --help
Output of above command is as shown below (F: 5)
As we can see in above image, using the -l option we can specify the language of the project. Currently only C# and F# languages are supported. Visual Basic support is coming soon. Using -t option we can specify the type of the project.
Currently four types of projects are supported:
If we don’t specify the values for -l and -t parameters explicitly while executing the dotnet new command, then default language is C# and default project type is Console application.
To create the first Hello World console application using command line tooling, let`s first create a folder called AspnetCore using the following command on any hard drive.
Type the following command to switch to our newly created directory.
Let`s again create a new folder called HelloWorld using following command.
Type the following command to switch to HelloWorld directory.
After that type the following command and hit enter.
dotnet new or dotnet new -l C# -t Console
Output of above command is as shown below (F: 6):
As we can see in above image, we have successfully created a new HelloWorld console application. Type dir command and hit enter to see the numbers of files in our new project. Output of dir command is as shown below (F: 7).
As we can see in above image, our new project just contains two files:
We can use any editor of our choice to edit these files.
Let`s use Visual Studio Code which is a cross platform editor which works on Windows, Linux and Mac OS. Type the following command in command prompt and hit enter to open the HelloWorld project in Visual Studio Code.
Once the project is open in visual studio code, double click on Program.cs file to open it. You will see some red squiggly lines and a popup message from Visual Studio Code that “There are unresolved dependencies from project.json. Please execute the Restore command to continue” as shown below (F: 8).
We can click on Restore button as shown in above image or execute dotnet restore command in command prompt to download the dependencies from nuget. Even if we click on Restore button in Visual Studio Code, behind the scenes it will execute the dotnet restore command for us and will restore the dependencies.
Output of restore command is as shown below (F: 9)
Once the dependencies are resolved, red squiggly lines will get removed. Now the final step is to run the program. We can run the program using dotnet run command. To execute it, we can either go back to our command prompt or Visual Studio Code also has integrated command prompt which we can open using Ctrl+` shortcut key. Once the command prompt is open, type the dotnet run command to execute the application.
Output of above command is as shown below (F: 10)
As we can see in above image Hello World message is printed out to console. Great we have successfully created a console application using dotnet core command line tooling and executed it.
In this article, we learned about how to get started with .NET Core Command Line Tooling and created a basic Hello World console application. I hope you enjoyed reading the article.
Nice article !! ... waiting for web application article in .net core ...
Thank you for the feedback Tushar.