Working With ASP.NET Core From Command Line

In this article, let's see how to work with ASP.NET Core from Command Line.

First, we have to see the installation folder of ASP.NET Core; I have installed it in “E Drive”. Open the command prompt in Administrative mode and type “dotnet --help”.

ASP.NET Core

Press Enter to see all the options.

ASP.NET Core

Here, we can find all the SDK commands like new, restore, run, and build etc. including the description of each of the commands.

We can test one by one how it works.

First, we will use the commands which are compulsory to run a project.
  • New – This command is used to create a new project
  • Restore – It restores all dependencies, packages required to build the project.
  • Build – This command is used to build the project.
  • Run – Once the build is succeeded, we can run the project by using this command. It is nothing but F5 in Visual Studio.

To see what templates are available to create a project, type “dotnet new” command. It will list out all the available templates, such as - Console Application, Class library, ASP.NET Core Web App etc. as shown in the below figure.

ASP.NET Core

By using Short Name, we can create a project directly from the command line.

I am going to create a console application. Follow the below steps.

Step 1

Let’s make a new directory for our project by typing “mkdir HelloDotNetCore”.

ASP.NET Core

Step 2

Change the current directory to the newly created folder.

ASP.NET Core

Step 3

Now, create a console application by using the command -

“dotnet new console”

ASP.NET Core

We can see the message - The template “Console Application" was created successfully. It will automatically run the restore command after creating the new project. It restores what minimum files are required to run a project.

Now, let’s go and check in the physical directory.

ASP.NET Core

We can see the project file, program.cs file, etc. So now, let’s build the application.

Step 3

Type “dotnet build” in the command line.

ASP.NET Core

The “Build succeeded" message will be shown. Now, run the application to see the output.

Step 4

Type “dotnet run” in the command line.

ASP.NET Core

We can see the default “Hello World!” message.

Let’s modify this and run once again.

Step 5

Type “notepad program.cs” to see the code.

ASP.NET Core

Modify the message to “Hello DotNetCore” and save the Notepad.

ASP.NET Core

Now, run the application once again to see the new output.

Type “dotnet run”.

ASP.NET Core

Now, we can see the “Hello DotNetCore” message.

Here, without using any IDE like Visual Studio, we are able to create and run a project using the command prompt.

Up Next
    Ebook Download
    View all
    Learn
    View all