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 macOS


In this recipe, we will learn how to install the .NET Core components in order to create an ASP.NET Core MVC application on macOS.

Getting ready

Because macOS is based on Unix, it is possible to run a .NET application on it with the Mono project. As mentioned in the previous recipe, for the moment, .NET application needs Mono to run on Linux or macOS.

To install Mono, we use Homebrew (http://brew.sh/), which is a package manager for macOS.

Note

We suppose that you have the necessary privileges to execute all these commands. If not, prefix sudo before all the command lines and you should be good to go.

How to do it...

In this recipe, we will do the following:

  1. Install Homebrew by entering this command at a Terminal:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 
  1. Use Homebrew to get DNVM, DNX, and DNUusing the commands as follows:
$ brew tap aspnet/dnx 

$ brew update 

$ brew install dnvm 

$ source dnvm.sh
  1. Install Node.js and npm (the npm will be installed automatically with Node.js):
$ brew install node
  1. Verify Node.js and NPM versions:
$ node -v 

$ npm -v

The version currently being used will be displayed on your screen as follows:

  1. Install Yeoman with Gulp to help automate tasks:
$ npm install -g yo gulp 

$ npm install -g generator-aspnet
  1. Yeoman asks us what type of application we want to create. In our case, we choose Web Application Basic.
  2. After that, Yeoman asks us the name for the application to create, as shown in the following screenshot.
  3. Yeoman will generate all the necessary files to run an ASP.NET Core MVC application. After the project is created, Yeoman shows us the different commands we can use to run the application.
  4. With the command cd "nameoftheapplication", we are placed in the application directory.
  1. After that, we can run the dotnet restore command, which will restore all the dependencies needed to run the application. In the background, this command will connect to the NuGet online repository to download, as NuGet packages the missing dependencies:
  1. Build and run webapp1 typing the following command:
$ dotnet run

After executing the preceding command you get the following output at localhost:5000: