Page 42 - MSDN Magazine, September 2017
P. 42

ASP.NET CORE
Simpler ASP.NET MVC
Apps with Razor Pages
Steve Smith
Razor Pages are a new feature in ASP.NET Core 2.0. They provide a simpler way to organize code within ASP.NET Core applications, keeping implementation logic and view models closer to the view implementation code. They also offer a simpler way to get started developing ASP.NET Core apps, but that doesn’t mean you should dismiss them if you’re an experienced .NET developer. You can also use Razor Pages to improve the organization of larger and more complex ASP.NET Core apps.
The Model-View-Controller (MVC) pattern is a mature UI pattern that Microsoft has supported for developing ASP.NET applications since 2009. It offers a number of benefits that can help application developers achieve a separation of concerns, result- ing in more maintainable software. Unfortunately, the pattern as
implemented in the default project templates often results in a lot of files and folders, which can add friction to development, espe- cially as an application grows. In my September 2016 article, I wrote about using Feature Slices as one approach to address this issue (msdn.com/magazine/mt763233). Razor Pages offer a new and different way to tackle this same problem, especially for scenarios that are conceptually page-based. This approach is especially useful when all you have is a nearly static view, or a simple form that just needs to perform a POST-Redirect-GET. These scenarios are the sweet spot for Razor Pages, which avoid a great deal of the convention required by MVC apps.
Getting Started with Razor Pages
To get started using Razor Pages, you can create a new ASP.NET Core Web Application in Visual Studio using ASP.NET Core 2.0, and select the Razor Pages template, as shown in Figure 1.
Youcanachievethesamethingfromthedotnetcommand-line interface (CLI) using:
dotnet new razor
You’ll need to make sure you’re running at least version 2.0 of the .NET Core SDK; check with:
dotnet --version
In either case, if you examine the project produced, you’ll see it includes a new folder, Pages, as shown in Figure 2.
Notably absent from this template are two folders that are typically associated with MVC projects: Controllers and Views. Razor Pages use the Pages folder to hold all of the pages for the application. You’re
This article discusses:
• Getting started using Razor Pages
• Demonstrating with a sample project
• Routing, model binding and handlers
• Using filters
• The Razor Pages architectural pattern
Technologies discussed:
ASP.NET Core 2.0, Razor Pages, Web UI Patterns and Architecture
Code download available at:
bit.ly/2eJ01cS
34 msdn magazine









































































   40   41   42   43   44