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.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.

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


- 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?

- Mike


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

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....