Book Image

ASP.NET Core MVC 2.0 Cookbook

By : Jason De Oliveira, Engin Polat, Stephane Belkheraz
Book Image

ASP.NET Core MVC 2.0 Cookbook

By: Jason De Oliveira, Engin Polat, Stephane Belkheraz

Overview of this book

The ASP.NET Core 2.0 Framework has been designed to meet all the needs of today’s web developers. It provides better control, support for test-driven development, and cleaner code. Moreover, it’s lightweight and allows you to run apps on Windows, OSX and Linux, making it the most popular web framework with modern day developers. This book takes a unique approach to web development, using real-world examples to guide you through problems with ASP.NET Core 2.0 web applications. It covers Visual Studio 2017- and ASP.NET Core 2.0-specifc changes and provides general MVC development recipes. It explores setting up .NET Core, Visual Studio 2017, Node.js modules, and NuGet. Next, it shows you how to work with Inversion of Control data pattern and caching. We explore everyday ASP.NET Core MVC 2.0 patterns and go beyond it into troubleshooting. Finally, we lead you through migrating, hosting, and deploying your code. By the end of the book, you’ll not only have explored every aspect of ASP.NET Core MVC 2.0, you’ll also have a reference you can keep coming back to whenever you need to get the job done.
Table of Contents (26 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating an ASP.NET Core MVC application on Linux


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.

Getting ready

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.

How to do it...

After installing .NET Core on Linux, creating a new project is easy.

Let's start creating a new project:

  1. 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
  1. Next, we need to run dotnet restore in the project folder. Most IDEs run dotnet restore in the background for us, such as Visual Studio and Visual Studio Code

How it works...

The dotnet CLI will generate all the necessary files you need for the project template we select.

The dotnet run command will be used to launch the ASP.NET Core MVC project: