In this recipe, we will create an ASP.NET Core MVC application on Linux. In order to perform this feature, we will have to install all the necessary libraries, components, and IDE on Ubuntu.
For cost consideration, we may be interested in hosting our websites on a Linux infrastructure. With cloud possibilities, we can consider hosting a Linux VM with ASP.NET Core capabilities.
For this recipe, we will use Ubuntu 17.10, but you can use a different Linux distribution such as Debian, CentOS, Fedora or any Linux distribution that supports a Docker Engine.
Before .NET Core, there was the Mono
Framework (http://www.mono-project.com/) to run a .NET application on Linux. Mono
is an open source and a cross-platform port of the .NET
Framework, which contains an Apache
module to host ASP.NET applications.
For now, .NET applications always need Mono
to run on Linux or OS X, because not all the BCL (The .NET
Framework base class library) is fully ported on Linux.
To host our ASP.NET Core 2.0 application on Linux, we will use Kestrel (you can read more about this at https://github.com/aspnet/KestrelHttpServer) as a WebServer
, which is a lightweight cross-platform web server able to execute ASP.NET Core code. Kestrel uses Libuv internally as a multi-platform asynchronous IO
and TCP
library, also used by Node.js.
For the moment, there's no other web server we could use to host ASP.NET Core applications on Linux or macOS, and Kestrel is not production-ready. We can also use IIS or WebListener
as a web server to host ASP.NET Core applications, but exclusively on Windows.
After installing .NET Core on Linux, creating a new project is easy.
Let's start creating a new project:
- Run
dotnet new mvc
from the command line. This command will create a new ASP.NET Core MVC project with the same name as the containing folder. If we want to give a different name to the project,-n PROJECTNAME switch
should be added
- Next, we need to run
dotnet restore
in the project folder. Most IDEs rundotnet restore
in the background for us, such as Visual Studio and Visual Studio Code