Welcome Razor Pages

Aug 15, 2016     Viewed 6663 times    2 Comments
Posted in #Razor 

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'> 
                <p>@episode.Title</p> 
                <div class='description'> 
                    <p>@episode.Summary</p> 
                </div> 
            </div> 
        </a> 
    </div> 
    <audio id="audio-@episode.Slug" preload="none" src="http://podtrac.com/pts/redirect.mp3/traffic.libsyn.com/devlife/@HtmlEncoder.Encode(episode.MediaFile)"></audio>
    }
</div>

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

Twitter Facebook Google + LinkedIn


2 Comments

Keith (8/26/2016 6:34:05 PM)

Hey
You probably didn't build this with that intent but have you thought about contributing these changes to Hanselman's repo?
It would be nice to have an actual production website running on Razor Pages that people can use for reference.

Hisham Bin Ateya (8/28/2016 5:20:32 AM)

@Keith my intent is to build a real website using Razor Pages, I find "This Developer's Life" podcast is a good show case for that. FYI I contacted Scott Hanselman personally, he is happy to having it in my repository.
Having this in production is very nice, but may needs time to convince (not enforce) Scott because "Razor Pages" is still a prototype and its in the early stage.


Leave a Comment