Razor Pages - The Elevator Pitch

In the last ASP.NET Community Standup, Jon Galloway gave my introductory Razor Pages article a shout out (thank you, Jon). During the discussion that ensued, Scott Hanselman asked for the "elevator pitch" for Razor Pages. Well, here's mine.

First, I think it's worth repeating what Razor Pages isn't, according to Damian Edwards, the driving force behind Razor Pages. It isn't ASP.NET Web Pages or any kind of successor to WebMatrix. There are undeniable similarities between Razor Pages and Web Pages (which was only ever intended as an entry-level framework) so there is always the possibility that Razor Pages will be dismissed as something only suitable for trivial applications, and not really robust enough for serious application development. After all, that's what MVC is for, isn't it? Proper application development? It must be, because the framework is based on a recognised design pattern, Model-View-Controller. QED.

Well here's a couple of other design patterns, straight out of Martin Fowler's Patterns Of Enterprise Application Architecture (so they are proper, recognised design patterns to be taken just as seriously as MVC):

  • Page Controller (pp 333-343) - an object that handles a request for a specific page or action on a web site
  • Template View (pp 350-360) - Renders information into HTML be embedding markers in an HTML page

Razor Pages makes use of both patterns, so if being based on documented design patterns is really an endorsement, Razor Pages is right up there with MVC.

The truth is that there is nothing inherent in the MVC or Page Controller patterns that make them more or less suitable for underpinning bigger applications. It's how you design the framework that matters. Frameworks are suitable for larger scale application development if they promote separation of concerns and are lightweight. This was a key driver behind the introduction of ASP.NET MVC back in the day. And the .NET Core version of MVC has really embraced this with the inclusion of Dependency Injection as a first class feature.

The good news is that Razor Pages offers exactly the same level of support for separation of concerns as the MVCframework. To illustrate this, here's a simple service based on an interface:

public interface IClock
{
    string GetTime();
}
using System;

public class LocalClock : IClock
{
    public string GetTime()
    {
        return $"The local time is {DateTime.Now}";
    }
}

This can be registered in the ConfigureServices method of the StartUp class, using the default ASP.NET Core dependency injection system:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddTransient<IClock, LocalClock>();
}

Now that the service is registered, it is available for injection into the PageModel constructor in a page controller file:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RazorPagesTest.Pages
{
    public class AboutModel : PageModel
    {
        private IClock _clock;

        public AboutModel(IClock clock)
        {
            _clock = clock;
        }

        public string Message { get; set; }

        public void OnGet()
        {
            Message = _clock.GetTime();
        }
    }
}

Quickly run the app to prove the concept:

DI in ASP.NET Core

Now this demo service is extremely simple, but it could just as easily be part of a complex business logic layer, or a repository (if you don't mind injecting those into controllers). The key take-away from this should be that Razor Pages is just as capable of supporting clear separation of concerns and granularity of code as MVC. There is no reason whatsoever why Razor Pages can't be used for large scale application development.

Why Choose Razor Pages?

So why is Razor Pages needed? Well, many people are a lot more comfortable with the Page Controller pattern, where URLs typically map to physical files on disk, and the logic for processing requests is married to the respective file. Some struggle conceptually with the Model View Controller pattern. Razor Pages will make it much easier for those people to develop cross-platform applications using the .NET Core framework.

Razor Pages uses a lot of the primitives from ASP.NET Core MVC, so the learning curve for any MVC developer should be about 10 minutes. That's not to say that Razor Pages is intended to replace MVC, but it should not be dismissed lightly as a beginner's framework suitable only for simple web sites. It is a lot more than that. Despite that, it also offers a lot for the beginner.