Razor Pages - The Elevator Pitch

5 (6 votes)

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.

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 4282

8 Comments

- Cyrus

There are some concern about razor pages performance. Is it faster or slower than MVC? would you please prepare a benchmark (for a same simple request) to compare request per second?
cheers

- Mike

@Cyrus

If people have concerns about performance, they should do their own tests and present the results to the ASP.NET team on Github if they find issues. The team are always testing for performance, and at the moment, Razor Pages is not finished. It's still only on Preview 1. There are currently at least 2 more previews planned, so a lot of work is still to be done.

- Obinna Okafor

Thank you for this wonderful piece, Mike.

I need more of these...Keep them coming - anything Razor Pages.

Once more thank you.

- Mau

I am missing the razor view code.
Thank you for your article. Design pattern should improve the development process instead of slowing it down. This gives the developer more flexibility without have to use viewcomponents (partial views) for everything. Misusing could send your code back to active server pages though.

- ojorma

Finally I can say goodbye to webforms

- Harris Boyce

As a developer of a couple "trivial" web pages applications used by non-profits that wouldn't have an application otherwise I really resent that concept. In my it's unfortunate that the power of simplicity brought ttithe ASP.NET platform by web pages will be lost in ASP.NET Core going forward. That said, I really appreciate the the articles you are contributing to greater understanding of Razor pages

- Dale Severin

I work with razor web pages extensively. I appreciate the rapid development it permits me to accomplish. I am sad that ASP.NET Core razor pages are not as simple as the razor web pages used in ASP.NET 4.5. If I need to go to the linux environment, I may have to use php again.

- Satyabrata Mohapatra

@Dale Severin
You can continue to build apps using asp.net web pages.

Recent Comments

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...