Razor Pages In ASP.NET Core

Razor Pages is a new feature of ASP.NET Core MVC. We can use this approach when there are some page-focused scenarios.

Creating Razor Pages in ASP.NET Core

For creating this project, you should have .NET Core installed and Visual Studio 2015 or later.

In Visual Studio, select File -> New -> Project.

Now, create new ASP.NET Core Web Application and name it as RazoPagesDemo.

ASP.NET Core

Click OK and select the latest ASP.NET Core in the drop-down. Then, select ASP.NET Core 2.0 and select Web Application in the next window.

ASP.NET Core

Click OK. Visual Studio will create a project.
 
In the Solution Explorer, you can see there is a folder named pages and all the views are present in that folder.

ASP.NET Core

Now, run the project. You will see a basic Razor page template in the browser.

Project Structure

  • wwwroot
    This folder contains all client-side files like CSS and JavaScript that are static.

  • Pages
    This folder contains all the Razor pages of the project.

  • appsettings.json
    This is the configuration file.

  • bower.json
    It is used for client side package management.

  • Program.cs
    It hosts the web app.

  • Startup.cs
    It configures the application’s services and request pipelines.  

How Razor Pages works

First, let us create our own test Razor page.

Right click on the Pages folder and add a new Razor page.

ASP.NET Core

It will create a new Razor page named test.cshtml. Expand the View; you will see a cs file with the same name.

This cs file is called code-behind file.

ASP.NET Core

Copy the following code in your test page.

  1. @page  
  2. @model testModel  
  3. @using Microsoft.AspNetCore.Mvc.RazorPages  
  4. @functions {  
  5.     public class testModel : PageModel  
  6.     {  
  7.         public string Message { get; private set; } = "In test page model: ";  
  8.         public void OnGet()  
  9.         {  
  10.             Message += $" Current Time  { DateTime.Now.ToString() }";  
  11.         }  
  12.     }  
  13. }  
  14. <h2>In page sample</h2>  
  15. <p>  
  16.     @Model.Message  
  17. </p>  

Save the file and "Run" your project. It will run on some port. Navigate to this test page.

http://localhost:x/test.

You will see the following screen in the browser.

ASP.NET Core
Now, if you see the code of the Razor page, it seems like Razor View file. But the difference is that it has a @Page directive that makes the file an MVC action. It handles the request directly without going to the Controller. @Page must be the first directive in any Razor Page. @function directive can be used for binding the model with the page. But the same thing can be done by using the code behind file.
 
Now, let us move our @function directive code to code-behind file. Open the code-behind file and copy the code in cs file.
  1. using Microsoft.AspNetCore.Mvc.RazorPages;  
  2. using System;  
  3.   
  4. namespace RazorPages.Pages  
  5. {  
  6.     public class testModel : PageModel  
  7.     {  
  8.         public string Message { get; private set; } = "In test page model: ";  
  9.         public void OnGet()  
  10.         {  
  11.             Message += $" Code Behind Current Time  { DateTime.Now.ToString() }";  
  12.         }  
  13.     }  
  14. }  

Now, your test Razor Page will look like this.

  1. @page  
  2. @model testModel  
  3. @using Microsoft.AspNetCore.Mvc.RazorPages  
  4. <h2>In page sample</h2>  
  5. <p>  
  6.     @Model.Message  
  7. </p>  

Save all the files and Run the project again. The following output will be shown in the browser.

ASP.NET Core
When we run the project, it will look for the named Razor page in the Pages folder. By default, it will show the index page.