Search code examples
asp.netasp.net-coreasp.net-core-mvcasp.net-core-mvc-2.0razor-pages

Why is Razor Pages the recommended approach to create a Web UI in Asp.net Core?


Learning new things needs an investment of time, space and energy. I am currently learning Asp.Net Core MVC 2.0. This ASP.NET Core tutorials overview states:

Razor Pages is the recommended approach to create a Web UI with ASP.NET Core

This information confused me in deciding whether I have to stop learning Asp.net Core MVC and start learning Asp.net Core Razor Pages.

  • Why is Razor Pages the recommended approach to create a Web UI in Asp.net Core?

Any directions are welcome.


Solution

  • Razor Pages are optimized for page-based workflows and can be used in these scenarios with fewer moving parts than traditional MVC models. This is because you don't need to deal with Controllers, Actions, Routes, ViewModels, and Views (as you typically would). Instead your route is convention-based, and your PageModel serves as your Controller, Action(s), and ViewModel all in one. The page, of course, replaces the View. You also don't have to have as many folders as you would in MVC, further simplifying your project.

    From ASP.NET Core - Simpler ASP.NET MVC Apps with Razor Pages, a Sept. 2017 MSDN article by Steve Smith:

    [Razor Pages] provide

    • a simpler way to organize code within ASP.NET Core applications, keeping implementation logic and view models closer to the view implementation code.
    • They also offer a simpler way to get started developing ASP.NET Core apps,

    That article has more information on why to use Razor Pages over MVC for page-based workflows. Obviously, for APIs, you will still want to use Controllers.

    3rd party edit - disadvantages of classical MVC folder organization

    ASP.NET Core - Feature Slices for ASP.NET Core MVC, an older MSDN article from Sept. 2016, describes why the classical MVC convention to organize views and controller might have disadvantages for larger projects. The article gives an example of four loosely related application concepts: Ninjas, Plants, Pirates and Zombies. The article outlines a way to structure them outside of the default folder convention by organizing files into folders by feature or area of responsibility.