Which one is better for your project - Razor Pages or MVC ?


Before .Net Core 2.0 we had MVC option to develop a web application. After the release of .Net Core 2.0, a developer has one more option to create a Web Application in .Net Core that is called .Net Core Razor pages.
In this blog, we are going to see the differences between Web Application (Razor Pages) and Web Application (MVC) in ASP.Net Core. We will also see which kind of Web Application is well suited for your project or requirement by looking at benefits and code comparison.

Recently I was creating a project in ASP.Net Core 2.1. I found 2 options in ASP.Net Core Web Application as below -

Web Application - A project template for creating an ASP.Net Core application with example ASP.Net Core Razor Pages content.

Web Application (Model-View-Controller) - A project template for creating an ASP.Net Core application with example ASP.Net Core MVC View and Controllers. This template can also be used for RESTful HTTP services.

I scratched my head and was unable to decide which option I should select.
So I decided to find out the core differences between these two so that we can easily find out the differences between Razor Pages and MVC.

Our Popular Blog -

By the end of this article, you will see the pros and cons of these two also you will be able to decide which type of pages you should choose for your project.

Razor Pages vs MVC

ASP.Net Core Razor Page is very similar to ASP.NET MVC's view pages. It has a similarity in syntax and functionality as MVC. The major difference is that in an MVC application model and controller code is included or bonded with View pages.

Razor pages are the next evolution of traditional ASP.Net WebForms. MVC is good for those web application which has lots of dynamic server pages, REST APIs and AJAX call.
For basic web pages and simple user input forms, we should go for Razor pages.

Razor pages are similar to ASP.Net web forms in the context of structure, except page extension.
In ASP.Net web forms, we had .aspx for UI and .aspx.cs file for code behind, in ASP.Net Razor pages we have .cshtml for UI and .cshtml.cs for code behind.

So, we can say that Razor pages are more organized compared to MVC. In MVC lots of routing get involved with Model, Controller, and Actions within Controller. But this is not the case with Razor pages.

We have a Razor view and code-behind file for this view to manage all actions and events on the View page.

Structure of Razor Pages and MVC

Razor pages do not have any kind of structure like MVC.  All Razor pages reside under the Pages folder with a very basic structure. You may see the below screenshot. Further, you can organize your project structure based on your functionality.

The below screenshot will explain to you about differences between the project structure of .Net Core Razor pages and MVC.




Code Comparison

As I have already mentioned above that Razor pages and MVC look almost similar i.e. both having .cshtml as the file extension.

If you will notice the 2 pages, one from Razor and another from MVC then you will notice @page at the top of Razor pages.

@page is a directive used in Razor pages.
Another difference is the model declaration in Razor pages.

In Razor pages model declaration is like

For index.cshtml - @model IndexModel
For about.cshtml - @model AboutModel

Adding a new Razor Page in application

Right-click on Pages folder in Solution Explorer and Select Add->Razor Pages

Select Razor Page from below screen and click Add


Next, Give a name to the view and select the required options and click Add.



Default Code-behind for index.cshtml

public class IndexModel : PageModel
    {
        public void OnGet()
        {

        }
    }


There is no code-behind in MVC. In MVC, the developer has to use the controller file to create an Action method to link a particular View.

Page Routing

In Razor pages, default routing will find a Razor page from the Pages folder for a specific request. Means routing configuration will try to find the same name in the Pages folder for which a request was generated.

So, if a user requests/home then routing will try to find home in the pages folder.

But this is not the case in MVC.
In MVC default routing configuration will try to find a matching controller and action to load a view.

Advantages of using Razor Pages

There are few advantages of using Razor pages over MVC. Razor pages are easy to understand in the context of structure. Razor pages do not have a controller and action to load.
Razor pages contain code behind for individual pages.

Summary:
Razor pages are more structured and easy to understand compare to MVC. Razor pages are similar to traditional ASPX pages as Razor pages contain code-behind files too. Razor pages have @page directive at the top of the page.

There could be lots of debate for deciding which one is better.
But, if you want to create a website with static pages or with a few dynamic pages, I think the Razor page is a good choice.

But If you are creating a REST-based API project or lots and lots of dynamic pages, you should go for MVC.

If you like this blog, please share this on Social media.

Previous Blog - Hosting an ASP.Net Core application on IIS
Next Blog - ASP.Net Core Razor Pages handler methods

You may like other blogs -

Interview Questions and Answers Series -

MVC Interview Questions and Answers
Web API interview questions and answers

Comments

Popular