Build your first ASP.NET Core web app

This is a very simple tutorial and 101 guide on getting an ASP.NET Core web application up and running. We’re starting from first principles, and we won’t get very deep into the vast world of ASP.NET, so you won’t find any details on topics like MVC, Routing or Entity Frameworks here. Our goal is to get a very simple web app & server running on your Windows/Linux/OSX machine

Install & setup .NET Core

We’ll need a couple of things before we start. Firstly the .NET Core SDK. Don’t worry ‘Core’ is very lightweight compared to the older “Full” .NET so no huge frameworks to install, but we still need some tools to work with. Download what you need from and follow the few steps there to get it set-up (which varies a little between Windows, Linux and OSX).

Now if you open a CMD / terminal and run dotnet you should see something like this

Project Setup & First Run

OK baby steps… Now let’s create a new .NET project

Now we’ve got a minimal skeleton project to work with, namely a Program.cs file and project.json. It’s not much, but hey it’s a start. Let’s try running our app

Whoops! This will result in a slightly cryptic error about a missing lock file, and needing to run something called a “restore”. Remember I said .NET Core was lightweight? well part of that is it only pulls in the components and libraries it really needs. This is handled by the project.json file declaring dependencies and those dependencies being pulled down from the online NuGet repository. Don’t worry too much at this point, the dotnet restore command does everything you need, so….

Well look it’s our old friend “Hello World”, wonderful.

Webify Things

If we want to take a look at our Program.cs file we can use any text editor you like (well, except Emacs you weirdo) I would suggest Visual Studio Code (aka VS Code). “Errg! Visual Studio?! Won’t that be a 6GB download, clog my machine up with junk, and cost £1,000s?”. Nope, VS Code is a great lightweight code editor and IDE for Windows, Linux and OSX. It’s more akin to Atom or Sublime Text than its name would suggest. It’s not just for C# and .NET, has a tonne of plugins and language support and fantastic git integration (far better than Atom‘s IMO).

Opening Program.cs in VS Code for the first time you’ll probably be prompted to install some plugins for .NET and C#, these aren’t essential but I recommended to click yes. VS Code treats folders as projects, so you are best off opening the ‘webapp’ folder on your filesystem rather than individual files

Let’s turn our classic Hello World console app into a web application. This is not difficult and will take 2 minutes, just follow the steps from the ASP.NET Getting Started Guide.
Now you will have a Startup.cs and a modified project.json and Program.cs. When you run the program (either with dotnet run command or pressing F5 in VS Code) you should see output like this:

And something like this in your browser:


Ok great, we’ve got a web application & web server running in about 15 lines of code. And none of the normal overhead you’d expect with ASP. No Windows Server, no IIS, no .NET Frameworks, no W3SVC or web.config XML file hell. Pretty cool IMO

You might be thinking, “This seems a bit pedestrian and low level, surely ASP.NET can let me start with a more functional web app without all this grunt work?”. Yes it can, back when we ran dotnet new, we could have run dotnet new –type web and we’ve have got a fully functional bootstrap MVC web app, with all the stuff you’d expect like; routing, template based views, a model and scaffolded database, etc etc.

Listen for external requests

Wait don’t leave! There’s one little thing we need to do, which will be required later when we want to run our app in a Docker container and access it. The web host used by our app is Kestrel which is a minimal HTTP server builtin to .NET Core. By default it only binds to the loopback adapter (i.e. localhost), which will be a problem later when we want to access the app from the outside. It’s simple to fix, just add .UseUrls(“http://*:5000”) to your WebHostBuilder in Program.cs


Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *