In this article, I will discuss Razor Pages vs MVC on how to choose in ASP.NET Core web applications. You will also see which type of web application is well suited for your project or requirement by looking at benefits and code comparison. Keep reading on How to integrate AdminLTE with ASP.NET Core 3.1
When you create a new ASP.Net Core web application there are two different types of options available as shown below:-
- Web Application (Razor Pages)
- Web Application (Model-View-Controller)
Razor Pages vs MVC
- A Razor Page is very similar to ASP.NET MVC’s view component. It has basically the same syntax and functionality as MVC. The key difference between Razor pages and MVC is that the model and controller code is also included within the Razor Page itself.
- MVC is good for those web application which has lots of dynamic server views, single-page apps, REST APIs, and AJAX calls. but Razor Pages are perfect for simple static pages that are read-only or do basic data input.
- Now, the ASP.NET MVC has been extremely popular for web application development, and it definitely has its benefits. But, the new ASP.NET Core Razor Pages is the next evolution of ASP.NET WebForms.
Structure of Razor Pages and MVC Project
The following pictorial representation will explain to you about differences between the project structure of .Net Core Razor pages and MVC.
As you can see razor pages do not have any kind of structure like MVC. All Razor pages reside under the Pages folder with a very basic structure.
- Keep reading on CRUD Operation with Razor Pages in ASP.Net Core 3.1
A Quick Comparison of How Requests Are Handled in Both
MVC: Here’s a quick overview of how MVC handles the requests.
As you can see, the default configuration for routing is the combination of action and controller names. So if you request for /staff/index, then it will route you the action named Index on the StaffController class.
But, it can be customized or configured to route any request to any controller with a block of code.
Razor Pages: Here’s a quick overview of how Razor Pages handle the requests.
The difference between the two is that in Razor Pages when you make a request, the default routing configuration will find a Razor Page for that specific request in the Pages folder.
Let say if you make a request for /contact/, then ASP.NET Core will look for a page having the same name that you used in request and will route you directly to it.
That means, a request to /contact/ will route you to Contact.cshtml
And for any .cshtml file to be considered as a Razor Page, it must be placed in the Pages folder and also contain @Page in its markup. This way, the Razor Page will then act as a controller action.
I hope you liked this article on the difference between Razor pages vs MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.