Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Razor pages vs Full MVC Core [closed]

There has been a question at SO Why is Razor Pages the recommended approach to create a Web UI in Asp.net Core 2.0? where Steve Smith has kindly explained the benefits of using Razor Pages over full MVC from the perspective of having less files.

I've been using Razor Pages for a while and noticed that despite an advantage of a Razor Page simplicity, it is a bit complicated when it comes to custom routing, structuring folders and complex view model (page model seem to be cluttered).

So, the questions are:

  1. If there is anything else except simplicity of a page to prefer Razor Pages over Controllers/Views - specifically I'm interested in performance of the two frameworks?
  2. Is it acceptable to combine Razor Pages and Controllers/Views at the same time?

I would also appreciate if some of expirienced guys shared your thoughts (pros and cons) about using Razor Pages to better understand this framework.

like image 922
Ivan Zaruba Avatar asked Jan 05 '18 22:01

Ivan Zaruba


People also ask

Is Razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Should I learn Razor pages or MVC?

Razor Pages represents a simpler way to generate HTML on the server compared to MVC. It is recommended for all new web applications that rely on server-side HTML generation going forward. MVC is still available for existing apps. It is also probably easier to migrate older MVC 5 (.

Can you briefly explain the advantages or disadvantages of Razor pages over MVC?

The main difference between MVC and Razor pages is the fact that the controller code and the model are likewise included inside the Razor Page. Simply speaking, it is almost similar to an MVVM framework providing an easier development experience as well as 2-way data binding with isolated concerns.

Should I use Razor pages?

Advantages of Razor Pages:Simple context of structure and has no controllers. Flexibility to fit any application you want to build. It has specific codes behind individual pages and is more organized. Build web apps in less time just like you did in ASP.NET Webforms.


2 Answers

We've recently launched a pretty decent sized app using Razor Pages for the front end and MVC controllers for the API for client side components. My experience has been this:

The pages paradigm works well when your content is structured around the idea of actual "pages" on the site. Think about things such as a Contact Us or an About or even a Login page. Sure, those could be done via MVC, but MVC is really unnecessary. A simple page will suffice. Leave the controllers to more controller'ish things like a product catalog or a user database.

If your MVC architecture revolves heavily around your view structure, razor pages is probably a good fit. You can still use the MVC bits for API related stuff, but the benefit of pages is that your front end structure becomes more explicit and less implicit ("convention-based") like with MVC where each action could or could not have a view that is typically named after the action.

like image 87
Chris Avatar answered Oct 16 '22 13:10

Chris


Although Chris has provided a clear opinion on the frameworks being shipped with a standard ASP.NET Core Implementation. People must also note that for MS to recommend Razor Pages there's rather deeper reasons for that and they have been clear to me since I'm a big fan of razor pages.

If there is anything else except simplicity of a page to prefer Razor Pages over Controllers/Views - specifically I'm interested in performance of the two frameworks?

  • To answer this, You sound like you simply haven't done a really heavy app in MVC if you're asking this question. Controllers become flocked with code, and it could be really messy, considering that not all programmers out there are following good programming practices(commenting and proper code management)... So imagine a controller with a few Page methods with logic all put in it. Well, the result is you'd easily end up with a controller consisting of over 400+ lines of code that could easily be split as it refers to different pieces of the app(pages or methods). So How about breaking those 300+ lines of code and separating them based on what they do? This is the whole idea behind razor pages. You only put code related to a specific page to it's own model rather than mixing code related to 10 pages in one file. Well, others could argue that you can create external classes where you'd put this logic to keep the controllers clean, but why go the extra mile? and secondly performance shouldn't be an issue at all. I don't think it matters at this point. Razor Pages thrives when it comes to form-based pages since model binding is so smooth with it.

Is it acceptable to combine Razor Pages and Controllers/Views at the same time?

  • Well, MVC is the pinnacle of WebAPI's when it comes to .NET. So the two can be mixed at any point, anytime. Razor pages truly resemble "true pages" but they're flexible to fit anything, either a catalog or whatever you wish to build. each page can serve it's own requests GET, POST etc... so due to that you can do whatever you want with a razor page. It must also be clear that routing isn't complicated with Razor Pages. It works just as flexible as in MVC...

I've been using Razor Pages for a while and noticed that despite an advantage of a Razor Page simplicity, it is a bit complicated when it comes to custom routing, structuring folders and complex view model (page model seem to be cluttered).

  • I'd rather disagree with this. Routing is flexible with Razor pages, you just haven't worked yourself around it. Also a razor page can build a very complex View because you can Bind as many View Models to it without any extra-effort required. With the annotation rule [BindProperties] or [BindProperty], you can import as many fields you want into a view... and remember that the binding is Two-Way.

Unless you start working with Razor pages, you'll hear a lot of opinions about it, some will sway you away from using it, some like Chris will give you true feedback. But my advise is, Razor pages has evolved and performs best in any application of any size...

I hope you find this interesting to read.

.Net Core Family

like image 27
Mosia Thabo Avatar answered Oct 16 '22 11:10

Mosia Thabo