locked
ASP.NET Core MVC or Razor Pages? RRS feed

  • Question

  • User-954747950 posted

    Hello everyone,

    I'm new to web development and decided to pick ASP.NET Core for a project since I have prior knowledge of C#. I don't have any experience with JS, but I have a good grasp of OOP. I will be writing a basic reservation system for a tourism company, but we will add other features in the future, so it is not going to stay as a small project. Considering this, should I go with MVC pattern or should I stick to razor pages and learn that instead?

    Sunday, February 23, 2020 8:34 PM

Answers

  • User-821857111 posted

    Razor Pages is the recommended way to build server-side websites these days. It's a lot easier to work with than MVC and is just as powerful. Microsoft recommend that controllers should only be used for Web APIs.

    Here's a starting point: https://www.learnrazorpages.com

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 7:51 PM
  • User-821857111 posted

    You would organise a Razor Pages application in a similar way to an MVC application. The traditional Presentation - Business Logic - Data Access layer approach is just as valid for Razor Pages.  

    I usually create a "service" layer (Business Logic) that calls methods in a Data Access layer (repositories, Entity Framework or whatever) and call the service layer methods in the PageModel handler methods. In a large application, I might even create the layers as separate projects. 

    The cshtml file is a template for generating HTML on the server to be sent to the browser. You would only likely use Web API controllers in a Razor Pages application if you want to move the processing and UI generation to the client (using JavaScript). Web API endpoints return data (as JSON by default), not HTML. They can be useful if parts of your page are driven by AJAX. There's an example here: https://www.learnrazorpages.com/web-api

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 9:12 PM

All replies

  • User-821857111 posted

    Razor Pages is the recommended way to build server-side websites these days. It's a lot easier to work with than MVC and is just as powerful. Microsoft recommend that controllers should only be used for Web APIs.

    Here's a starting point: https://www.learnrazorpages.com

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 7:51 PM
  • User-954747950 posted

    Thank you for your answer! I've started experimenting with Razor Pages and the web site you mentioned has been very helpful. Now I have couple more questions:

    How should the project structure be? I only have one project and I am writing all the code for business logic in cshtml.cs files for that specific page at the moment. This includes database operations. I have a Model folder for my models and a Data folder that contains my Db Context, but other than that, everything is under the Pages folder, in their respective subfolders. I believe there are times I move away from single responsibility principle and sometimes it feels like the code won't be expandable and maintainable even in the short term. I would be glad if you can give me some tips about code design and structuring using Razor Pages.

    Also, related to above, I am kind of confused about where the Razor Pages stands in terms of back-end and front-end development. Should I create another project and write a Web API for the site and use Razor Pages only for UI? The good thing is I am moving forward very quickly like this, but I also worry about problems I might encounter as the project gets bigger and more complicated.

    Tuesday, February 25, 2020 8:40 PM
  • User-17257777 posted

    Hi replicator2699,

    Razor Pages provides an abstraction over the top of MVC, which makes it better suited to some page-based apps. 

    You can take a look at the difference between Razor Pages and traditional MVC and have a better understand how do they work.

    https://www.twilio.com/blog/introduction-asp-net-core-razor-pages

    Best Regards,

    Jidong Meng

    Wednesday, February 26, 2020 7:06 AM
  • User-821857111 posted

    You would organise a Razor Pages application in a similar way to an MVC application. The traditional Presentation - Business Logic - Data Access layer approach is just as valid for Razor Pages.  

    I usually create a "service" layer (Business Logic) that calls methods in a Data Access layer (repositories, Entity Framework or whatever) and call the service layer methods in the PageModel handler methods. In a large application, I might even create the layers as separate projects. 

    The cshtml file is a template for generating HTML on the server to be sent to the browser. You would only likely use Web API controllers in a Razor Pages application if you want to move the processing and UI generation to the client (using JavaScript). Web API endpoints return data (as JSON by default), not HTML. They can be useful if parts of your page are driven by AJAX. There's an example here: https://www.learnrazorpages.com/web-api

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 26, 2020 9:12 PM