Page 32 - MSDN Magazine, September 2017
P. 32

ASP.NET CORE
Getting Started with
ASP.NET Core 2.0
Mike Rousos
ASP.NETCoremakesiteasytocreatefast,portable, cross-platform Web applications. This article will guide you through developing a simple ASP.NET Core Web site and will show what role each file in the project plays. It will also explain important ASP.NET Core concepts along the way. There will be a special focusonchangesinASP.NETCore2.0tohelpreadersfamiliarwith ASP.NET Core 1.0 and 1.1 make the transition to 2.0.
Creating an ASP.NET Core Project
ASP.NETCoreprojectscanbecreatedfromtemplatesusingeither Visual Studio or the .NET Core command-line interface (.NET CLI). Visual Studio 2017 gives a great .NET Core development experience—with top-notch debugging, Docker integration and many other features—but I’ll use the .NET CLI and Visual Studio Code in this walk-through, in case some of you want to follow along on a Mac or Linux dev machine.
The dotnet new command is used to create new .NET Core proj- ects. Running dotnet new without any additional arguments will
list the available project templates, as shown in Figure 1. If you’re familiar with previous versions of the .NET CLI, you’ll notice a number of new templates available in version 2.0.
Angular and React.js SPA Templates: These templates create an ASP.NET Core application that serves a single-page application (using either Angular 4 or React.js) as its front end. The templates include both the front-end and back-end applications, as well as a Webpack configuration to build the front end (and csproj mod- ifications to kick off the Webpack build every time the ASP.NET Core project is built).
ASP.NET Core Web App with Razor Pages: Razor Pages is a new ASP.NET Core 2.0 feature that allows you to create pages that can handle requests directly (without needing a controller). These are a great option for scenarios that fit a page-based programming model.
For this walk-through, let’s start with the new Razor Pages template by executing dotnet new razor. As soon as the project has been created, you should be able to run it by executing dotnet run. In previous versions of .NET Core, it would’ve been necessary to execute dotnet restore first to install the necessary NuGet pack- ages. But beginning with .NET Core 2.0, the restore command is now automatically run by CLI commands that depend on it. Go ahead and test the template Web site out by executing dotnet run and navigating to the URL on which the app is listening (likely http://localhost:5000). You should see the Web app rendered (as in Figure 2).
Congratulations on launching your first ASP.NET Core 2.0 app! Now that you have a simple Web app running, let’s take a
This article discusses:
• Creating an ASP.NET Core project
• Creating and running the Web host
• Dependency injection and the request-processing pipeline • What’s new in ASP.NET Core 2.0
Technologies discussed:
ASP.NET Core 2.0, .NET Core Project Templates, Razor Pages
28 msdn magazine















































































   30   31   32   33   34