Razor pages are page-based programming model for ASP.NET Core. It's still a prototype in the early stages, hosted in GitHub in this repo, proving the concept of MVC View Pages which already have a long discussion here.

Razor Page came up to simplifying page-focused scenarios, which is very useful in some cases such as live.asp.net website, which has two pages if I'm not wrong, so instead of the MVC that we use and love, this can be simplified with this new model.

What are Razor Pages trying to do & What are not?

The razor pages are trying to:

  • Make dynamic html and forms easier in ASP.NET Core.
  • Reduce the number of folders and files in MVC structure that requires for page-focused scenarios.
  • Simplify the code for page-focused pattern.
  • Enable to return non html responses when necessary. 
  • Use MVC primitives as much as possible.
  • Simplify the migration to traditional MVC structure.

The razor pages are not trying to:

  • Create scripted page framework to compete with PHP .. etc.
  • Hide C# with Domain Specification Language (DSL) in razor or otherwise.
  • Create new primitives that only applicable for this model.

Real World Sample

Let us see some real code and where razor page may fit, I was thinking a lot for appropriate sample that I can demo, I remembered that Scott Hanselman has a great podcast called This Developer's Life, so I downloaded the source code for his repo, and starting to build the podcast again in ASP.NET Core with Razor Page model.

The code in my repo is straightforward, the structure is clean and simple, if we take a quick look to the image below we will notice that Models and Views folders are

same in what we have seen in MVC, there's no Controllers folder, because this model is page-focused so that is why we have Pages folder that contains both .razor and .cs which represent the code behind, this remind me the Web Forms where we have .aspx and .aspx.cs, but the idea is here like what's known before ASP.NET Web Pages.

Some may wonder why we have Views folder?!! the views folder may contains layout pages, partials .. etc. The main pages should be in the Pages folder till now.

Let us see some real code

@functions {
    public IEnumerable<Episode> RecentEpisodes {get; set;}
    public async Task OnGetAsync()
        RecentEpisodes = GetRecentEpisodes();

    Layout = "~/Views/Shared/_SiteLayout.cshtml";
    ViewBag.Title = "Stories About Developers and Their Lives";

<div class= "container-wide" style="margin-bottom:36px">
    @foreach(var episode in RecentEpisodes){
    <div style="margin-right:22px;float:left;margin-bottom:22px;margin-top:36px" class="episode"> 
        <a href='/Show?slug=@episode.Slug' class="showlink"> 
            <img src="@Href("~/Images/" + episode.LeadImage)"
                alt="@episode.Title" width="320" height="220"/>
            <div class='title'> 
                <div class='description'> 
    <audio id="audio-@episode.Slug" preload="none" src="http://podtrac.com/pts/redirect.mp3/traffic.libsyn.com/devlife/@HtmlEncoder.Encode(episode.MediaFile)"></audio>

the code above is default page which shows all the episodes of the show, if we notice almost the logic code should go inside @functions block and .cs file below, and the rest of the page is a normal view that contains razor syntax.

namespace ThisDevelopersLife.Pages
    public class Index : Page
        public IEnumerable<Episode> GetRecentEpisodes()
            return ShowDatabase.Recent(10);

The coolest part in Razor Page is the notion of model binding, and if you have a deep look you will find a method called OnGetAsync also there's OnPostAsync which they handle the GET and POST actions.

The Razor Pages have the simplicity of the ASP.NET Web Page to implement page-focus scenarios and brings almost the goodness that ASP.NET MVC have such as model binding, model validation, html helpers, tag helpers and much more.