Your first .NET Core web application using just the command line (in ten minutes or less)

July 18, 2016 · 5 minute read

So you want to mess around with .NET Core, maybe create a Web API or MVC Project and you don’t want to rely on any tooling, just the command line.

Happily .NET Core makes it really easy to get started.

If you’re going to skip Visual Studio and work via the command line or Visual Studio Code you only need to download the .NET Core SDK for Windows

With that installed, it’s time to build your first application.

Open up a command prompt (or use the in-built terminal in Visual Studio Code) and start by creating a folder for your application, then initialising it.

mkdir CoreApp
cd CoreApp
dotnet new console

When you run the dotnet command for the first time you’ll see some information about how .NET Core collects usage data and initially populates a local package cache. Once you’ve seen this message you won’t see it again on the same machine.

Going it alone

To really understand the nuts and bolts of how ASP.NET Core (and .NET Core) work, we’re starting with the smallest .NET Core application you can possibly create, before bringing in ASP.NET Core and MVC manually.

But, if you want, you can take a shortcut. To start with ASP.NET Core and MVC already set up and configured, use this command instead.

dotnet new mvc

And now you can jump straight to part 3 - compile your changes on the fly.

Is that it?

Still here?

Good, so let’s explore this simplest of .NET Core apps.

One of the most striking things about getting started with .NET Core is that you don’t end up with lots of files and dependencies right out of the gate, just the minimum you need for an empty web site.

In fact, all you’ll be left with is a .csproj and Program.cs file.

img

At this point, this is just a .net Core app (not web) which prints “Hello World” to the console.

If you take a look at CoreApp.csproj, you’ll see that very little configuration is needed to get up and runnning. This one simply states that the compiled file will be an executable targeting .NET Core 1.1

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

</Project>

Your .NET Core app requires the core .NET libraries in order to run, but doesn’t download them automatically. To rectify this, issue these commands to restore the dependencies and then to run the app.

dotnet restore
dotnet run

You promised me the world (well the web at least)

So your app just compiled and launched. It looks suspiciously like a console application, because it is a console application.

Up until now, everything you’ve done has resulted in a minimal .net core console application.

So how do you turn it into a web site?

Well you need to bring in ASP.NET Core and Kestrel HTTP server as dependencies.

Kestrel is a lightning fast cross-platform web server which can be used to self-host your web application. In other words, you can tell your new web app that Kestrel is a dependency and then run your site on it without relying on IIS or IIS Express.

There are two ways to add these dependencies to your project.

Via the command line

dotnet add package Microsoft.AspNetCore.Server.Kestrel -v 1.1.* 
dotnet add package Microsoft.AspNetCore -v 1.1.* 

This will update .csproj for you with the latest 1.1.x versions of the Kestrel web server and ASP.NET Core package.

By manually editing csproj

Or make the changes to .csproj yourself.

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.*" />
  <PackageReference Include="Microsoft.AspNetCore" Version="1.1.*" />
</ItemGroup>

You’ve introduced new dependencies to your app, so now you need to download them using the restore command.

dotnet restore

Incidentally, in case you’re wondering where donet restore is restoring packages to, the default location is %userprofile%\.nuget\packages

Next up, you’ll need to create a Startup.cs file. This will define how incoming web requests should be handled.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace CoreApp {
    public class Startup{

        public void Configure(IApplicationBuilder app){
            app.Run(context => {
                return context.Response.WriteAsync("Hello world");
            });
            
        }
    }
}

This is about as simple as it comes, you simply tell your app to always return a response and write the text “Hello world” to it (for any request to your web application).

At this point, you haven’t told your app to start Kestrel (to start accepting web requests). You can do that by updating Program.cs.

using System;
using Microsoft.AspNetCore.Hosting;

namespace CoreApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();

            host.Run();
        }
    }
}

Should you wish, you can happily delete Console.WriteLine(“Hello World!”) as you’ve replaced it with something far more useful, you’ve told Core to launch Kestrel using the Startup class you just created.

Go ahead and run your app.

dotnet run

You’ll see a message telling you that your app is up and running and where you can access it.

Hosting environment: Development
Content root path: C:\Users\Jon\CoreApp\bin\Debug\netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Hit http://localhost:5000 in a browser to see the fruits of your hard work!

Hello World in the browser

Not bad for a few minutes work!

Next time we’ll take a look at adding MVC and Web API.

Join the Practical ASP.NET Newsletter

Ship better Blazor apps, faster. One practical tip every Tuesday.

I respect your email privacy. Unsubscribe with one click.