New feature of .NET Core 2 – Razor Pages

By Dawid on (tags: .NET Core 2.0, Pages, razor, categories: code)

If you are looking for another way of building web applications then Razor Pages are for you! Razor Pages are page based programming model for ASP.NET Core MVC and they makes page focused scenarios easier and more productive.

Razor pages are enabled by default in MVC and you don’t have to modify your Startup.cs code in any way. Assembly in which razor pages feature – Microsoft.AspNetCore.Mvc.PazorPages – should be referenced automatically if you are already using Microsoft.AspNetCore.Mvc.

Empty Razor Page looks as fallow:

   1: @page
   2:  
   3: @{
   4:     string text = "I'm Razor Page";
   5: }
   6:  
   7: <html>
   8: <body>
   9:     <div>@text</div>
  10: </body>
  11: </html>
 

As you can see the only difference from standard ASP.NET MVC page is @page directive. This turns this page into MVC action which can handle requests directly and no controller is needed.

Razor page with Page Model

While using Razor Pages feature we can specify model which is acting more like a view model rather then regular MVC model. Take a look bellow:

   1: public class MyModel : PageModel
   2: {
   3:     public string Message { get; set; }
   4:  
   5:     public void OnGet()
   6:     {
   7:         Message = "I'm your Razor Page";
   8:     }
   9: }

OnGet() is handler method that is called with GET-request. There is also similar handler available for POST-method and both of these methods have also asynchronous versions  - OnGetAsync() and OnPostAsync(). This feature is called Page Actions that works in a similar way to controller actions in MVC. Those methods can also take parameters which are hydrated by the model binding feature.

And here is how you can use you model:

   1: @page
   2: @model MyModel
   3: @{
   4:     ViewData["Title"] = "Example Razor Page";
   5: }
   6: <h1>@ViewData["Title"].</h1>
   7: <div>@Model.Message</div>

Razor page without code behind file

All the code which in previous example was placed in PageModel file can be moved to @functions section like this:

   1:  
   2: @page
   3: @{
   4:     ViewData["Title"] = "Example";
   5: }
   6: @functions {
   7:     public string Message { get; set; }
   8:  
   9:     public void OnGet()
  10:     {
  11:         Message = "I'm your Razor Page";
  12:     }
  13: }
  14: <h1>@ViewData["Title"].</h1>
  15: <div>@Message</div>
Pages work with all the features of the Razor view engine like layouts, partials, templates, tag helpers, _ViewStart.cshtml, _ViewImports.cshtml. All of those work in the same way they do for conventional Razor views.

Summary

It’s hard to say how many of you will use Razor Pages. For sure it is lightweight option for those who just want to get started.

If you thing of Razor Pages as of another page scripted framework you are wrong. What are Razor Pages trying to do is:

  • simplify the code for page focused patterns
  • makes dynamic HTML easier in ASP.NET Core MVC
  • they also some how reduce number of directories in MVC project structure
  • use MVC primitives as much as possible