Razor Pages - The Elevator Pitch

5 (4 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: 2916

5 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

Recent Comments

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...

Mau 06/06/2017 08:58
In response to Razor Pages - The Elevator Pitch
I am missing the razor view code. Thank you for your article. Design pattern should improve the of...

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...