For those of you old enough to remember Web Forms it represented a relatively straightforward way for drag and drop developers to migrate from the relative comforts of desktop development to the growing web development frontier. Of course what we found out quite quickly is that Web Forms was not magic and that some would even consider its fundamental design choices an unfortunate compromise.

However you choose to remember Web Forms it cannot be overstated that it was an incredibly important and liberating moment for Visual Studio and the developers that used it. Still, when ASP.NET MVC was introduced back in 2009 it promised "closer to the metal" web programming, with explicit control over the resulting HTML markup, which was a direct shot at Web Forms and the convoluted markup it produced (MVC was also open source).

Do we need Razor Pages?

I would think so! The promising way in which Razor Pages was conceived ensures that we have a relatively straightforward model for beginners but it also inherits the requisite complexity and integrity of a full MVC application, in this way I think it has a much better chance of assuming the preferred starting position for novice and veterans developers (unlike Web Pages).

ASP.NET Core 2 provides a new Razor Page template for creating a new project, but in the spirit of the cross platform CLI you could just as easily run this from your command line (assuming you installed .NET Core SDK 2):

dotnet new razor

Once you open the project you will probably immediately notice that there is no Controllers or Views folder. Instead you have a Pages folder and by convention it maps as follows:

Path and file name Relative URI
/Pages/Index.cshtml / or /Index
/Pages/Contact.cshtml /Contact
/Pages/Store/Contact.cshtml /Store/Contact
/Pages/Store/Index.cshtml /Store or /Store/Index

Pure MVC is also about convention, and conventions are not necessarily intuitive, and in some cases they requires a little mental gymnastics to imagine what might actually be occurring below the abstraction. With Razor Pages though they directly connected the relative location in the Pages folder to the route.

Anatomy of a Razor Page

If you have done any amount of MVC development you will notice the similarities to Views, what makes it different is the @page directive which makes the file into and MVC action:

@page

<h2>Hello, world!</h2>
<p>What's the time Mr. Wolf - @DateTime.Now</p>

Clearly this rudimentary example requires that standard HTML be interspersed with server code, however, Razor Pages also supports code behind file. So Pages/Contact.cshtml, can also have an accompanying code behind file in the form Pages/Contact.cshtml.cs. I could rewrite the previous example as two files, as in:

Pages/Contact.cshtml

@page
@using RazorPageExample
@model ContactModel

<h2>Hello, world!/h2>
<p>
    @Model.MessageTime
</p>

… and Pages/Contact.cshtml.cs

using Microsoft.AspNetCore.Mvc.RazorPages;
using System;

namespace RazorPageExample
{
    public class ContactModel : PageModel
    {
        public string MessageTime { get; private set; } = "What's the time Mr. Wolf - ";

        public void OnGetAync()
        {
            MessageTime += $"{ DateTime.Now }";
        }
    }
}

The above page has an OnGetAsync handler method, which runs on GET requests. You can add handler methods for any HTTP verb. The most common handlers are OnPost, OnGet, OnDelete etc. This, for me, is where it clearly surpasses the Web Forms in that we remain true to the HTTP verbs by using these handlers and we relegate the idea of things like postback and complex page to life cycles to the past.

Should I use Razor Page

Yes, well probably. As it relates it relates to the ASP.NET Core MVC version of DasBlog I am considering it for any page that does not rely on having data pulled from a repository layer, essentially wherever I think a Controller is unwarranted.

I would recommend Razor Pages as follow:-

  • If you are develop a relatively straightforward site that is one step more advance than static HTML but you want technology agnostic URIs.
  • If you think that the Controller is a bit overkill for the two page web site you are creating for your aunt, then Razor Pages maybe more appropriate.
  • Teaching a class to fresh new developers? Razor Page appears to be a great first step for anyone wanting to do basic server side development, even if you plan to eventually use full MVC.