Monday, April 2, 2018

How to Build and Run ASP.NET Core on Linux

Building and Running ASP.NET Core apps on Linux is fun. Let's review how that works.
Continuing the discussion about .NET Core, on this post, let's talk about building web applications using the .Net Core framework entirely on Linux. But before touching code, what is Asp.Net Core?


Asp.Net Core  is the evolution of Asp.Net, a web application framework developed by Microsoft to build web applications Apis and web services. Asp.Net Core was supposed to be ASP.NET v5 but since it's a full rewrite and was based on .Net Core Framework (and not on the .Net Framework), it was renamed to ASP.NET Core to mark as not being part of the ASP.NET framework.

(For a gentle introduction on Asp.Net Core, please visit this page).

Why ASP.NET Core?

Because it represents the next generation of the existing Asp.Net stack with true multi-platform, faster and with many other improvements.I will not list here what I found is a substantial change between Asp.Net and Asp.Net Core.

Running on Linux/BSD/mac

My favourite favourite feature is being able to run .NET Core and ASP.NET Core apps on Linux, my operating system of choice is another big another big advantage. Why? Because Linux powers most of the internet and is way more performant, secure and cheaper then equivalent Windows servers. Plus, being open source, allows us to engage better with the wholoe community including non-Microsoft shops.


Being able to host and run ASP.NET Core web apps on containers is my second favourite feature. In fact, it's only capable because containers are essentially embbedded in the Linux kernel. Running our web apps on containers mean our apps can be scaled out more easily, are more secure and can be run on modern clusterization tools such as Kubernetes with very little effort.


As part of .NET Core, Microsoft has created a new web server called Kestrel. The objectives of Kestrel are:
  • to facilitate development removing the dependency on IIS thus, allowing Asp.Net Core apps to run on environments that do not support IIS (Linux and Mac for example);
  • making web app deployments as easy as deploying a console app.
  • be cloud-ready.
Note that you should not use Kestrel in production as it does not have advanced features like virtual hosts, logging, security, etc;

Compatibility with the .Net Standard

.NET Core is compatible with .NET Framework making it adhere to the already existing .Net standard base, being compatible with Xamarin and Mono, via the .NET Standard.

Rich CLI

There are a lot of command-line tools that can be used to more easily create the command dotnet is the entry for them.We can start interacting with the dotnet tool by:
dotnet new --help
On Linux, if issue the command above I get:

And More

  • The framework is completely distributed as NuGet packages
  • Cloud-optimized runtime
  • A unified story for building web UI and web APIs
  • A cloud-ready environment-based a rewritten configuration system
  • A lightweight and modular HTTP request pipeline
  • Build and run cross-platform ASP.NET Core apps on Windows, Mac, and Linux
  • Open-source
  • Side-by-side app versioning when targeting .NET Core.

Creating a simple Asp.Net Core app in Linux

So let's review two common use cases: creating a razor based core web app and an Agular .Net Core web app.

Creating a Razor based Asp.Net Core app

The simplest web app we can build in Asp.Net core is a razor web app. Assuming that we installed the templates described previously, the first command that we need to run is:

dotnet new razor -o razordemo

After cding into that folder, by running the command below I have my app running:

dotnet run
A scaffolded razor core web app running on Linux

Creating a Asp.Net Core SPA app

In order to create SPA web apps in .Net Core, we may need to install the Microsoft.AspNetCore.SpaTemplates nuget package. That package installs spa templates for the more common client side frameworks including Angular and React.js.

Some of the templates may use NPM and  WebPack 2 to bundle and deploy all client resources (JavaScript, html, images, and CSS). They can be installed with the following dotnet core command:

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*

Once installed with the command above, if you run dotnet new command you'll see your existing templates:
dotnet new --help

To create a new web app running Angular, for example, just run, as exemplified above:
dotnet new angular -o angulardemo

As informed, the installation is not ready yet. I still need to run "npm install" on the command line to finish package dependencies. So I cd into my angulardemo folder and run "npm install":

And, to finish up, just run dotnet run to start your web app pointing your browser to that address:

Final Thoughts

On this post we introduced Asp.Net Core development on Linux. I wanted to demo how easy it was to scaffold Asp.Net Core web apps entirely in Linux to show how mature .Net Core has gotten on non-Windows platforms. You can reproduce the exact same steps on your Mac if you want.

And remember: Linux is now a viable alternative for developers working with the .NET stack! Why don't you also give it a try yourself?


See Also

For more posts about .NET Core, please click here.
Do you have any comment or suggestion about this post? Please contact me @BrunoHilden