Razor Pages From Zero To Hero - Part One

Razor Pages

 
Razor page is the most underrated technology in .NET Core ecosystem. Maybe it is due to lack of knowledge or maybe Microsoft is not able to make a place for this in developers’ mind. This will be a series of posts where we will learn about Razor pages; it will be from basic to advanced concepts. Razor pages run on .NET Core and use MVC framework under the hood. So, do not think of it as new technology; it is a part of MVC so you’re getting all the free goodies which come with .NET Core and MVC. The basic difference between MVC and Razor pages is that Razor pages a take page-centric approach instead of controller and action-centric in MVC.
 
So, the first question that comes to mind is to find why I should use a different framework, not the existing familiar MVC. So below is the point which I feel is the advantage, based on my experience, of working with Razor Pages.
  • Single responsibility or Separate of concern.
  • Better manageability of code.
Let us discuss what I mean from Single responsibility here. The best way to understand this is to create a sample app. Let us keep it simple. For now, this app will not connect to DB and all the data will be in memory. This application is a Task management app where the user can do all CRUD operations on this. Now, let us create an application like below for MVC, and create a controller with all action methods to complete the CRUD functionality.
 
 
If you see above, this is how a controller looks. The controller will be in one folder and views will be in another folder. Sometimes, we just want to navigate to it without knowing the name. In that case, first, we need to find the name of the action; then from there, we can find the view. It is really hard sometimes. Imagine if I want to do some modification in the view why shall I go to the action.
 
 
As you can see from the screenshot, this controller is quite busy here. And the length is the first problem as it is not really easy to reach to the last action method. If you see here, this call is not doing one thing - it is trying to do many things here, i.e., create, delete, update, get in my view. It is breaking the Single responsibility principles. Let us now create the same functionality in Razor pages. The solution looks like below.
 
 
Just notice the different folder structure. Look at the folder called Pages and inside the folder, there is a folder called Task which contains 4 cshtml pages. For now, do not worry about the Pages folder. We will learn all the convention later. Just remember Pages is the folder where all the pages will go. By convention, the containing folder will be part of the URL route. Let me explain what I am trying to say. When the request comes from /Task/Index, it always points to the Task folder and Index page. So if you want to modify something in the Create functionality you can go directly to the page Create to do changes. According to me, this is a clear approach to do the development.
 

Conclusion

 
As said by David Fowler, "you will not see how powerful and easy to use Razor pages is unless you will start exploring". This is a different way of writing a web application in ASP.NET Core.
 
Please put your thoughts in the comments section.