In ASP.NET, most builders use one of many two architectural patterns to construct net Purposes: Razor Pages and Mannequin View Controller. They’re barely completely different in construction and improvement method.
This GitHub subject is why Microsoft tends to deliver the Razor Pages in entrance of .NET documentation. Extra folks commented towards Razor Pages, however others got here up with some benefits why selecting this structure.
I’ll attempt to provide you with a good comparability of those two architectural kinds.
Razor Pages is a brand new function launched in ASP.NET Core 2. This can be a new architectural type that desires to simplify the folder construction of the MVC mannequin. In Razor Pages, every web page is cut up into two recordsdata: the view (HTML) and the C# class. The C# class comprises the mannequin and the logic. This structure is called MVVM(Mannequin-View-ViewModel).
Razor Pages are thought of safer than MVC:
- The PageModel of a web page is virtually a view mannequin. So, you solely ship the required info to the view
- By default, AntiForgeryToken is required
- By utilizing the BindProperty attribute, you specify which properties might be certain by the person.
One other benefit of Razor Pages is the routing mechanism. A web page will use the folder as a route. You too can set the route using the Web page directive.
@web page "/contact" @utilizing Microsoft.AspNetCore.Http.Extensions @mannequin CNPGenerator.Pages.ContactModel
Many individuals blame Razor Pages as a result of they’re much like WebForm (the odd know-how). That is partially true as a result of each applied sciences concentrate on the web page. However apart from that, nothing is in widespread. Razor pages have solely two strategies: the GET and the POST strategies. Razor Pages share 70% of the code with the MVC. You possibly can rework a Razor Web page in a short time into the MVC type.
Razor Pages preserve the code extra organized. You could have the view mannequin within the web page class. So, don’t fear about discovering the correspondence view on your code.
Mannequin View Controller
The Mannequin-View-Management is a extra standard design that’s extensively used. Most server-side languages use this paradigm.
Because the title says, the MVC mannequin is separated into three predominant elements: controllers, fashions, and views. The controller handles the enter and communicates with the mannequin and the view. The view is chargeable for the person interface, and the mannequin represents the info and the enterprise logic.
The MVC mannequin works for API improvement. Because the title says, Razor Pages cope with pages, not APIs. For those who plan to make use of a front-end framework like Angular or React, then MVC is the proper selection.
What to decide on between Razor Pages and MVC?
I truthfully consider that in case you have robust information of MVC, observe that path. I don’t suppose you’re going to get a bonus by beginning to find out how Razor Pages work. Many builders start to hate the brand new applied sciences that wish to clear up some points of the present patterns. They get annoyed as a result of a brand new factor is launched each few years, and every part should be modified.
However, Razor Pages and Mannequin-View-Controller have many similarities. Each use the Razor Syntax for producing dynamic views. Additionally, each separate the person interface from the enterprise logic.
If you wish to begin a brand new net utility, select Razor Pages. I choose Razor Pages for small web sites. For instance, you wish to create a web site with three pages. You need one thing small with just some logic and a pleasant person interface. Then the only option for this kind of utility is Razor Pages.
Probably the most important benefits of Razor Pages is that it scales higher than MVC. Utilizing the MVC mannequin will make you navigate between folders in case you have an intensive net utility. Razor Pages are extra light-weight than the MVC mannequin.
One other method is to make use of each paradigms. For instance, the Identification framework makes use of Razor pages, however you’ll be able to have the remainder of the applying within the MVC. Additionally, Razor Pages doesn’t match API Improvement.
Most tasks can profit from each architectures. So my greatest recommendation for you is to make use of each architectural designs. In conclusion, the successful within the battle of the Razor Pages vs MVC is each.