Creating A New .NET Core Web Application With Command Line Interface (CLI)

First, you might be thinking, without using Visual Studio IDE, then are we going to use Command Prompt [Command Line Interface (CLI)]?


Tools Required

  1. NET Core 1.1 SDK

For direct downloading .NET Core 1.1 tools SDK, go through the URL - https://go.microsoft.com/fwlink/?LinkID=835014


Fig 1. Installing .Net Core 1.1 SDK

After installing new .NET Core 1.1 SDK -


Fig 2. Installed .Net Core 1.1 SDK

After installing, we will create first ASP.NET web application using Command Line Interface (CLI). Before getting started, let’s have a look at basic commands for creating a web application using CLI.

Note - For demo, just start Command Prompt as an Administrator.

Commands

  1. dotnet

    The Main command for running command line arguments. This command will return .Net Core Version and Build information.

    Example

    In Command Prompt, type “dotnet” and press enter.


    Fig 3. Running command “dotnet”

  2. dotnet –help

    This command will show all the information of common commands and arguments we can use.


    Fig 4. Running command “dotnet --help”

  3. dotnet new –t web

    This command will create MVC web application template.


    Fig 5. Running command “dotnet new –t web”

  4. dotnet restore

    This command will restore all dependencies.


    Fig 6. Running command “dotnet restore”

  5. dotnet build

    This command will compile the code.


    Fig 7. Running command “dotnet build”

  6. dotnet run

    This command will run the web application.


    Fig 8. Running command “dotnet run”

  7. dotnet publish

    This command is used for publishing a project.


    Fig 9. Running command “dotnet publish”

Getting Started

Step 1 Creating Folder

For storing all the files, we need a folder and for that, we are going to create the folder first.

For creating a folder, choose a proper location. I have chosen desktop.


Fig 10. Creating Demoapp folder

Now, you can check your desktop and there will be a folder with the name “Demoapp”.

Step 2

Now let’s get into newly created directory “Demoapp”.


Fig 11. Changing directory

Step 3

Now, create a web application with .Net Core; for doing this, you need to enter command “dotnet new -t web” and press enter.


Fig 12. Creating web application

Now, open the “Demoapp” folder and have a look on what is generated there.


Fig 13. Demo app folder view after creating project

Oh! It has generated MVC project -- that’s cool. The next step is to restore the dependencies for a demoapp application.

Step 4

Now, let’s restore dependencies of this project which are created by typing command “dotnet restore”. It will restore all the required dependencies.


Fig 14. Restoring dependencies

Next, we are going to build demoapp application.

Step 5

To build the application, type “dotnet build”.


Fig 15. Compiling application

Step 6

To run this application, type “dotnet run”.


Fig 16. Running application

Now, the server is listening on http://localhost:5000/ port.

Just copy and paste this URL into the browser.


Fig 17. The first view of .Net core application

Now, we have created our first .NET Core application, using Command Line Interface (CLI).

Step 7

To publish this application, type “dotnet publish”.


Fig 18. Publishing application

After publishing the demo application, now, let’s view files which are published.

Note - You will see the path of published files in green color.


Fig 19. Files which are published

Finally, in this article, we have learned how to create a web application using .NET Core.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now