dotnet / AspNetCore.Docs

Documentation for ASP.NET Core
https://docs.microsoft.com/aspnet/core
Creative Commons Attribution 4.0 International
12.64k stars 25.29k forks source link

Razor Pages vs MVC with controller and views - how do I decide #3889

Closed Rick-Anderson closed 2 years ago

Rick-Anderson commented 7 years ago

See Public review URL Internal review URL

We need a short section on this. (perhaps in the RP intro - but will need to link to it) Perhaps to include:

If you're familiar with the MVC approach, continue app development featuring controllers and views. You can add Razor Pages to your controller/view ASP.NET Core project. The controller-view approach to MVC may be preferred approach taken by many larger teams. The ASP.NET Core framework will continue to add improvements to those who using the controllers/views.

If you're new to ASP.NET Core, consider using Razor Pages. Many developers find Razor Pages simpler and more productive than using controllers and views in ASP.NET Core MVC applications.

Feature Controller/Views Razor Pages
Action Filters x 2.1
testable x x
separation of concerns x x
areas x 2.1
Partial views x na

closed: 2.0 MVC features Razor Pages supports/doesn't support #3751 - This doc will contain feature table like the one above.

Plan. After 2.1 release, schedule skype with @DamianEdwards and @davidfowl

tdykstra commented 7 years ago

Why wouldn't this go in the RP intro?

Rick-Anderson commented 7 years ago

@DamianEdwards can you draft a paragraph on "Razor Pages vs MVC with controller and views - how do I decide?" ?

Rick-Anderson commented 7 years ago

@DamianEdwards can you give me an ETA when you can come up with the first draft?

ardalis commented 7 years ago

Fewer files and folders, organized by feature, not file type: Controllers/Views [ ] RP [x]

Rick-Anderson commented 7 years ago

@ardalis RP has more files I don't understand organized by feature, not file type.

ardalis commented 7 years ago

Typical MVC "page" requires Controller, ViewModel, View, usually each in separate folders or folder hierarchies (in the case of views). None of these are grouped together in any way.

RP has just a page and it's pagemodel, grouped together by VS and colocated in one folder. The page model replaces the controller and viewmodel, providing the function of both, without so many files or folders involved. See my MSDN article for more on this if you're so inclined.

Rick-Anderson commented 6 years ago

A real app will have more files with RP as you get 1 PageModel file per method, while you stuff them all in 1 controller with MVC. But they are organized better and have a built in view model. No need to ever use view data.

ardalis commented 6 years ago

Have you read?

?

Explains better than I can here.

LeonardoDaga commented 6 years ago

I'm designing my project using Razor. I would like to add a model to a partial view (let's say _LayoutNavbar) independent of the model used in the body, created in an independent class (in this case: _LayoutNavbarModel) respect to each model used by all the pages supported by the same layout that loads the partial view (in my case, a navbar). Do I just found the reason why I should use Controller/Views instead of Razor?

briangithex commented 6 years ago

@LeonardoDaga Could that be accomplished by layout pages and/or partial pages? I'm not asking this rhetorically; I honestly don't know.

Rick-Anderson commented 2 years ago

Fixed by Choose an ASP.NET Core web UI