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

Asp.Net Core  is the evolution of Asp.Net, a web application framework developed by Microsoft to build web applications, web APIs and web services. ASP.NET Core was renamed from ASP.NET v5 since it was a full rewrite to meet .NET Core's requirements.
For an in-depth introduction on ASP.NET Core, please check 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 such as:
  • 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.

Multi-platform

My favourite feature of ASP.NET Core is being multi-platform. That opens up the development to Mac and Linux. Being able to run on Linux is also an advantage because containers are Linux and running on Linux is faster, cheaper and more secure than on Windows.

Containers 

Being able to run on Linux also allows us to run ASP.NET Core web apps on containers. 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.

Kestrel

Kestrel is another nice addition to .NET Core. With it, ASP.NET websites no longer rely on IIS to run, are easier to run on containers and on the cloud.
You should not use Kestrel in production as it does not have outstanding performance neither supports advanced features like virtual hosts, logging, security, etc.

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:

    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 asp.net 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:
    dotnet run

    Conclusion

    On this post we introduced ASP.NET Core development on Linux. I hope it was clear to understand how easy it is to scaffold ASP.NET Core web apps entirely in Linux. I hope you now realize how mature the .NET Core framework has gotten on non-Windows platforms. You can reproduce the exact same steps on your Mac if you want. Linux is now a viable alternative for developers working with the .NET stack! Why don't you also give it a try yourself?

    References

        See Also

            For more posts about .NET Core, please click here.

            Share

            Any comment about this page? Please reach out on Twitter