How to create an ASP.NET Core Web Application using dotnet-cli

NET Core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of pre-installed dotnet core project templates from which the user can create various applications as ASP.NET Core Web Application, WPF (Windows Presentation Foundation) application, console application, Windows Form application, and other test projects. The application created with dotnet-cli can be executed on any Windows, Linux, or macOS operating system.

How to create an “ASP.NET Core Web Application”

The user can use the dotnet new “ASP.NET Core Empty” or “dotnet new web” command from the dotnet-CLI toolset to create a new ASP.NET Core Web Application. The application does not have any content in it apart from boilerplate codes. The user can create a Web application with Angular, React, etc, from the list of available templates. You can check the installed templates by using dotnet new –list command. The command provides a few options that can be applied as a parameter to tweak the output of the command as shown below :

“dotnet new web –no-restore”: This command does not restore the project dependencies required by the ASP.NET Core Web application. Please follow through to read about a few “dotnet new web” command usage.

Prerequisite:

NET Core SDK 1.x and above. The current version is .NET Core 5.0 and can be downloaded here.

Command Syntax:

The format of the command is as follows: dotnet new web [options]

The details of the available command options can be listed using the –help option as follows:

C:\>dotnet new web --help
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


ASP.NET Core Empty (C#)
Author: Microsoft
Description: An empty project template for creating an ASP.NET Core application. This template does not have any content in it.
Options:
  --exclude-launch-settings  Whether to exclude launchSettings.json from the generated template.
                             bool - Optional
                             Default: false / (*) true

  --no-restore               If specified, skips the automatic restore of the project on create.
                             bool - Optional
                             Default: false / (*) true

  --no-https                 Whether to turn off HTTPS. This option only applies if Individual, IndividualB2C, SingleOrg, or MultiOrg aren't used for --auth.
                             bool - Optional
                             Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Command Usage – dotnet new web:

1. “dotnet new web”

The command creates a new “ASP.NET Core Web Application”, the project contains basic boilerplate files and directory. The name of the project is not specified in the command, by default the name of the project will be taken from the name of the folder the command has been executed in. The command also restores the dependencies required by the web project. As the default language is C#, a C# ASP.NET project will be created. On executing the application “Hello World” string is returned as a response. The list of files and folder created by the command are given below:

dotnet new web
C:\temp>dotnet new web
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on C:\temp\temp.csproj...
  Restore completed in 98.02 ms for C:\temp\temp.csproj.

Restore succeeded.

The list of the files and folders created:

C:\temp>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\temp

06/21/2019  06:59 PM    <DIR>          .
06/21/2019  06:59 PM    <DIR>          ..
06/21/2019  06:59 PM               146 appsettings.Development.json
06/21/2019  06:59 PM               192 appsettings.json
06/21/2019  06:59 PM    <DIR>          obj
06/21/2019  06:59 PM               712 Program.cs
06/21/2019  06:59 PM    <DIR>          Properties
06/21/2019  06:59 PM             1,284 Startup.cs
06/21/2019  06:59 PM               148 temp.csproj

Content of Startup.cs

C:\temp>type Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace temp
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

The content of the file might differ as per your installed SDK.

2. “dotnet new web –name FirstWebApplication”

The command creates a directory name FirstWebApplication if already not exists and then creates a new “ASP.NET Core Web Application” with the name of FirstWebApplication in it, additionally, the command also restores the dependencies required by the project. The list of files and directories created by the command are given below:

C:\>dotnet new web --name FirstWebApplication
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on FirstWebApplication\FirstWebApplication.csproj...
  Restore completed in 99.29 ms for C:\FirstWebApplication\FirstWebApplication.csproj.

Restore succeeded.

The list of files and folder created:

C:\FirstWebApplication>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\FirstWebApplication

06/21/2019  07:07 PM    <DIR>          .
06/21/2019  07:07 PM    <DIR>          ..
06/21/2019  07:06 PM               146 appsettings.Development.json
06/21/2019  07:06 PM               192 appsettings.json
06/21/2019  07:06 PM               148 FirstWebApplication.csproj
06/21/2019  07:07 PM    <DIR>          obj
06/21/2019  07:06 PM               727 Program.cs
06/21/2019  07:06 PM    <DIR>          Properties
06/21/2019  07:06 PM             1,299 Startup.cs

3. “dotnet new web –name FirstWebApplication –no-restore”

The command creates a directory name FirstWebApplication if already not exists and then creates a new “ASP.NET Core Web Application” in it, additionally, the command does not restore the dependencies required by the project. The list of files and directories created by the command are given below:

C:\>dotnet new web --name FirstWebApplication --no-restore
The template "ASP.NET Core Empty" was created successfully.

The list of files and folder created:

C:\FirstWebApplication>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\FirstWebApplication

06/21/2019  07:11 PM    <DIR>          .
06/21/2019  07:11 PM    <DIR>          ..
06/21/2019  07:11 PM               146 appsettings.Development.json
06/21/2019  07:11 PM               192 appsettings.json
06/21/2019  07:11 PM               148 FirstWebApplication.csproj
06/21/2019  07:11 PM               727 Program.cs
06/21/2019  07:11 PM    <DIR>          Properties
06/21/2019  07:11 PM             1,299 Startup.cs

4. “dotnet new web –name FirstWebApplication –language F#”

The command creates a directory name FirstWebApplication if already not exists and then creates a new “ASP.NET Core Web Application” in it, the programming language for the application is F#, and the extension of the project is .fsproj. Additionally, the command also restores the dependencies required for the project. The list of the files and folder created are given below:

C:\FirstWebApplication>dotnet new web --name FirstWebApplication --language F#
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on FirstWebApplication\FirstWebApplication.fsproj...
  Restore completed in 223.19 ms for C:\FirstWebApplication\FirstWebApplication\FirstWebApplication.fsproj.

Restore succeeded. 

The list of files and folders created:

C:\FirstWebApplication>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of C:\Temp\dotnet\FirstWebApplication

06/21/2019  07:16 PM    <DIR>          .
06/21/2019  07:16 PM    <DIR>          ..
06/21/2019  07:16 PM               146 appsettings.Development.json
06/21/2019  07:16 PM               157 appsettings.json
06/21/2019  07:16 PM               257 FirstWebApplication.fsproj
06/21/2019  07:16 PM    <DIR>          obj
06/21/2019  07:16 PM               676 Program.fs
06/21/2019  07:16 PM    <DIR>          Properties
06/21/2019  07:16 PM             1,041 Startup.fs

The content of Startup.fs

C:\FirstWebApplication>type Startup.fs

namespace FirstWebApplication

open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting

type Startup() =

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    member this.ConfigureServices(services: IServiceCollection) =
        ()

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    member this.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
        if env.IsDevelopment() then
            app.UseDeveloperExceptionPage() |> ignore

        app.UseRouting() |> ignore

        app.UseEndpoints(fun endpoints ->
            endpoints.MapGet("/", fun context -> context.Response.WriteAsync("Hello World!")) |> ignore
            ) |> ignore

The content of Program.fs

C:\FirstWebApplication>type Program.fs

namespace FirstWebApplication

open System
open System.Collections.Generic
open System.IO
open System.Linq
open System.Threading.Tasks
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging

module Program =
    let exitCode = 0

    let CreateHostBuilder args =
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(fun webBuilder ->
                webBuilder.UseStartup<Startup>() |> ignore
            )

    [<EntryPoint>]
    let main args =
        CreateHostBuilder(args).Build().Run()

        exitCode

5. “dotnet new web –name FirstWebApplication –no-https”

The command creates a directory name FirstWebApplication if it does not exists and then creates a new “ASP.NET Core Web Application” in it, additionally, the command restores the dependencies required by the project. With –no-https option the command does not assign SSL Port number and neither assigns a https application URL in the launchSettings.json, by default –no-https is false. The content of launchSettings.json with and without –no-https is given below.

a. With –no-https option:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:55408",

      "sslPort": 0   <<==  [[ SSL PORT VALUE IS 0 ]]

    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "FirstWebApplication": {
      "commandName": "Project",
      "launchBrowser": true,

      "applicationUrl": "http://localhost:5000",   <<==  [[ NO HTTPS URL ]]

      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

b. With default option: –no-https (false)

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:43814",

      "sslPort": 44385  <<==  [[ SSL PORT VALUE IS ASSIGNED ]]
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "FirstWebApplication": {
      "commandName": "Project",
      "launchBrowser": true,

      "applicationUrl": "https://localhost:5001;http://localhost:5000",  <<==  [[ HTTPS URL ]]

      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Download the latest version of NET Core or refer MSDN for further details

I hope you found this post on how to create an ASP.NET Core Web Application using dotnet-cli helpful. Thanks for visiting. Cheers!!!

[Further Readings: How to create a dotnet core NUnit Test Project using dotnet-cli |  How to create a dotnet core xUnit Test Project using dotnet-cli |  How to create a dotnet core MSTest Project using dotnet-cli |  How to create dotnet core WinForms Application using dotnet-cli |  How to create a dotnet core WPF application using dotnet-cli |  How to create a dotnet core console app using dotnet-cli |  Top 7 Web Frameworks to Learn and Focus on in 2021   |  Singleton Design Pattern in C#  |  Introduction to Design Patterns  |  How to add Git Bash to Windows Terminal Application  |  How to customize Windows Terminal Application  |  How to customize Windows Terminal Key Bindings    ]

4 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x