Page 49 - MSDN Magazine, September 2017
P. 49
After a handler has been selected, model binding occurs. After model binding, the OnPageHandlerExecuting method of any page filters is called. This method can access and manipulate any model-bound data available to the handler, and can short circuit the call to the handler. The OnPageHandlerExecuted method is then called after the handler has executed, but before the action result executes.
Conceptually, page filters are very similar to action filters, which run before and after actions execute.
Note that one filter, ValidateAntiforgeryToken, isn’t required for Razor Pages at all. This filter is used to protect against Cross- Site Request Forgery (CSRF or XSRF) attacks, but this protection is built into Razor Pages automatically.
Architectural Pattern
Razor Pages ship as part of ASP.NET Core MVC, and take advantage of many built-in ASP.NET Core MVC features like routing, model binding and filters. They share some naming similarity with the Web Pages feature that Microsoft shipped with Web Matrix in 2010. How- ever, while Web Pages primarily targeted novice Web developers (and were of little interest to most experienced developers), Razor Pages combine strong architectural design with approachability.
Architecturally, Razor Pages don’t follow the Model-View-Con- troller (MVC) pattern, because they lack Controllers. Rather, Razor Pages follow more of a Model-View-ViewModel (MVVM) pattern thatshouldbefamiliartomanynativeappdevelopers.Youcanalso consider Razor Pages to be an example of the Page Controller pat- tern, which Martin Fowler describes as “An object that handles a request for a specific page or action on a Web site. That [object] may be the page itself, or it may be a separate object that corresponds to that page.” Of course, the Page Controller pattern should also be familiar to anyone who has worked with ASP.NET Web Forms, becausethiswashowtheoriginalASP.NETpagesworked,aswell.
Unlike ASP.NET Web Forms, Razor Pages are built on ASP.NET Core and support loose coupling, separation of concerns and SOLID principles. Razor Pages are easily unit tested (if separate PageModel classes are used) and can provide a foundation for clean, maintainable enterprise applications. Don’t write off Razor Pages as just a “training wheels” feature meant for hobbyist pro- grammers. Give Razor Pages a serious look and consider whether Razor Pages (alone or in combination with traditional Controller and View pages) can improve the design of your ASP.NET Core application by reducing the number of folders you need to jump between when working on a particular feature.
Migrating
Although Razor Pages don’t follow the MVC pattern, they’re so closelycompatiblewiththeexistingASP.NETCoreMVCControl- lers and Views that switching between one and the other is usually very simple. To migrate existing Controller/View-based pages to use Razor Pages, follow these steps:
1. Copy the Razor View file to the appropriate location in the /Pages folder.
2. Add the @page directive to the View. If this was a GET-only View, you’re done.
3. Add a PageModel file named viewname.cshtml.cs and place it in the folder with the Razor Page.
4. If the View had a ViewModel, copy it to a PageModel file. 5. Copy any actions associated with the view from its
Controller to the PageModel class.
6. Rename the actions to use the Razor Pages handler syntax
(for example, “OnGet”).
7. Replace references to View helper methods with
Page methods.
8. Copy any constructor dependency injection code from the
Controller to the PageModel.
9. Replace code-passing model to views with a [BindProperty]
property on the PageModel.
10. Replace action method parameters accepting view model
objects with a [BindProperty] property, as well.
A well-factored MVC app will often have separate files for views, controllers, viewmodels, and binding models, usually each in sep- arate folders in the project. Razor Pages allow you to consolidate these concepts into a couple of linked files, in a single folder, while
still allowing your code to follow logical separation of concerns.
Razor Pages ship as part of ASP.NET Core MVC, and take advantage of many built-in ASP.NET Core MVC features like routing, model binding, and filters.
You should be able to reverse these steps to move from a Razor Pages implementation to a Controller/View-based approach, in most cases. Following these steps should work for most simple MVC-based actions and views. More complex applications may require additional steps and troubleshooting.
Next Steps
The sample includes four versions of the NinjaPiratePlantZombie organizer application, with support for adding and viewing each data type. The sample shows how to organize an app with several distinct functional areas using traditional MVC, MVC with Areas, MVC with Feature Slices and Razor Pages. Explore these differ- ent approaches and see which ones will work best in your own ASP.NET Core applications. The updated source code for this sam- pleisavailableatbit.ly/2eJ01cS. n
Steve Smith is an independent trainer, mentor and consultant. He is a 14-time Microsoft MVP award recipient, and works closely with several Microsoft product teams. Contact him at ardalis.com or on Twitter: @ardalis if your team is consid- ering a move to ASP.NET Core or if you’re looking to adopt better coding practices.
thankS to the following Microsoft technical expert for reviewing this article: Ryan Nowak
msdnmagazine.com
September 2017 41