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

Note. This is a re-write of a older post, as the .NET SDK and tooling have changed significantly upon final release in March 2017, making the old post more or less obsolete

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 https://www.microsoft.com/net/download/core#/sdk 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 Program.cs & Startup.cs files and webapp.csproj. It’s not much, but hey it’s a start.

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 9GB 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 either of the .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. Tip, in your terminal window you can just type  code .  to open VS Code with the current folder as your project.

Let’s try running our app from our terminal

Whoops! This will result in a slightly terrifying screen of red error text, and something about a missing “project.assets.json”. 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 .csproj file declaring dependencies and those dependencies being pulled down from the online NuGet repository as required. Don’t worry too much at this point, the dotnet restore command does everything you need, so….

And you’ll see something like this in your browser:

 

Ok great, it’s not earth shattering but we’ve got a web application & web server running in about 50 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 .NET Core can let me start with a more functional web app?”. Yes it can, back when we ran dotnet new, we could have run dotnet new mvc and we’ve have got a fully functional MVC web app using bootstrap, with all the stuff you’d expect like; routing, template based views, 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 host in Program.cs

 

Build your first ASP.NET Core web app

Leave a Reply

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