Skip to content

Conversation

Rick-Anderson
Copy link
Contributor

@Rick-Anderson Rick-Anderson commented Jul 24, 2018

Rick Anderson added 2 commits July 24, 2018 16:46
@Rick-Anderson
Copy link
Contributor Author

Rick-Anderson commented Jul 25, 2018

@scottsauber @sporty81 please review

Copy link
Contributor

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of great information here! I have some suggestions for reorganizing and supplementing it.


By [Scott Sauber](https://twitter.com/scottsauber) and [Rick Anderson](https://twitter.com/RickAndMSFT)

Razor Pages is a feature of the ASP.NET Core MVC framework. Razor Pages follows the MVVM pattern. Developers and teams familiar with ASP.NET MVC development:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link to an overview of MVVM?

Copy link
Contributor

@tdykstra tdykstra Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is an abrupt transition from starting to introduce RP to saying you can still use MVC. If this is aimed at MVC developers, I would save this theme for last and jump into the comparison here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's aimed at everyone with special attention to MVC developers. I need to assure them right off the bat that MVC/controllers/views is not going away and that we're continuously investing in it. Read #6146 to see what we're trying to address.

* Can continue app development featuring controllers and views.
* Can be assured the ASP.NET Core framework will provide improvements to controller/view development.

Razor Pages can be added to controller/view projects. The controller-view approach to MVC may be preferred by larger teams.
Copy link
Contributor

@tdykstra tdykstra Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto - I would save this for the end.


Razor Pages can be added to controller/view projects. The controller-view approach to MVC may be preferred by larger teams.

If you haven't tried Razor Pages, consider using them. Many developers find Razor Pages development simpler and more productive than using controllers and views.
Copy link
Contributor

@tdykstra tdykstra Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto again, save for the end.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Customers may find MVC more productive if they're heavily invested in it. Consider an ASP.NET 4.x app that was ported to ASP.NET Core 1.x. The team's devs can follow a pattern that's long been established when creating a new page in the app. My opinion is that it makes ASP.NET Core more approachable to beginners, since there are fewer moving parts. It's easier to ramp up on the tech because of this.


If you haven't tried Razor Pages, consider using them. Many developers find Razor Pages development simpler and more productive than using controllers and views.

Razor Pages advantages over controller/view development:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF we're going to present differences as advantages for RP, we should be sure to include differences that are advantages for MVC as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought on this comment occurs to me -- if we think MVC has no advantages, and if this article is really not so much about comparing the two as it is about "why RP is better than controllers/views and why you should try it", I would consider incorporating this content into the RP intro instead of this separate doc.


Razor Pages advantages over controller/view development:

* **A better folder structure by default that scales better.** In MVC, the default folder structure does not scale. Separate folders for Views, Controllers, and often ViewModels when all three are tightly coupled to one another. You must bounce between all three folders anytime you need to add or change a feature. With Razor Pages, your `PageModel` (Controller + ViewModel) are in the same folder as your View. You hit F7 in Visual Studio to toggle between them.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PageModel are --> PageModel is
does not scale --> is not easy to work with
(what you're describing isn't so much scaling as a structure that is intrinsically awkward to work with)

* All the Employee pages are under the */Employee* folder.
* The app can authorize an entire folder and require the user must be an Administrator to get to any subfolder of */Administrator*. The code to do this is more straightforward that than with multiple Controllers that make up the Administrator features.
* **More secure by default.** Razor Pages provides:
* [AntiForgeryToken validation](xref:razor-pages/index#xsrfcsrf-and-razor-pages) by default.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't MVC have a similar default?

* The app can authorize an entire folder and require the user must be an Administrator to get to any subfolder of */Administrator*. The code to do this is more straightforward that than with multiple Controllers that make up the Administrator features.
* **More secure by default.** Razor Pages provides:
* [AntiForgeryToken validation](xref:razor-pages/index#xsrfcsrf-and-razor-pages) by default.
* You opt-in to the properties that need to be model bound via `[BindProperty]`. `[BindProperty]` prevents over-posting attacks.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVC has the same feature, doesn't it?

* **More secure by default.** Razor Pages provides:
* [AntiForgeryToken validation](xref:razor-pages/index#xsrfcsrf-and-razor-pages) by default.
* You opt-in to the properties that need to be model bound via `[BindProperty]`. `[BindProperty]` prevents over-posting attacks.
* The `PageModel` acts like a view model.
Copy link
Contributor

@tdykstra tdykstra Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some explanation of how this is more secure vis-a-vis controllers/views would be helpful.


## Razor Pages compared to Web Forms

Razor Pages and Web Forms are page focused and each contain a *.cs* file associated with the markup page. Other than the page focus, Razor Pages are very different from Web Forms. It can be difficult to migrate Web Forms code to Razor Pages. Converting between Razor Pages and ASP.NET Core MVC is generally straightforward. Razor Pages and Web Forms have very little in common. Razor Pages and ASP.NET Core MVC share most of the ASP.NET Core framework. The following section shows many features shared between Razor Pages and ASP.NET Core MVC that are not found in Web Forms.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each contain --> each contains

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be made clear that Web Forms isn't supported in ASP.NET Core. This paragraph could lead a reader to believe it is supported. Maybe just prefix "Web Forms" with "ASP.NET 4.x"?

|areas| x | 2.1 |
|Partial views| x | na |
|View Components | x | x|
|Build in DI | x | x | No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abrupt ending here, some kind of next steps link(s) would be helpful.

I'd like to see a section on controller/views advantages. From this table I'd guess that partial views is a significant advantage.

description: Contrast and compare Razor Pages to controller/view programming in ASP.NET Core
monikerRange: '>= aspnetcore-2.0'
ms.author: riande
ms.date: 8/05/2018
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the metadata reference, the mm/dd/yyyy format should be used (08/05/2018).


Razor Pages can be added to controller/view projects. The controller-view approach to MVC may be preferred by larger teams.

If you haven't tried Razor Pages, consider using them. Many developers find Razor Pages development simpler and more productive than using controllers and views.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Customers may find MVC more productive if they're heavily invested in it. Consider an ASP.NET 4.x app that was ported to ASP.NET Core 1.x. The team's devs can follow a pattern that's long been established when creating a new page in the app. My opinion is that it makes ASP.NET Core more approachable to beginners, since there are fewer moving parts. It's easier to ramp up on the tech because of this.


## Razor Pages compared to Web Forms

Razor Pages and Web Forms are page focused and each contain a *.cs* file associated with the markup page. Other than the page focus, Razor Pages are very different from Web Forms. It can be difficult to migrate Web Forms code to Razor Pages. Converting between Razor Pages and ASP.NET Core MVC is generally straightforward. Razor Pages and Web Forms have very little in common. Razor Pages and ASP.NET Core MVC share most of the ASP.NET Core framework. The following section shows many features shared between Razor Pages and ASP.NET Core MVC that are not found in Web Forms.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be made clear that Web Forms isn't supported in ASP.NET Core. This paragraph could lead a reader to believe it is supported. Maybe just prefix "Web Forms" with "ASP.NET 4.x"?


Razor Pages advantages over controller/view development:

* **A better folder structure by default that scales better.** In MVC, the default folder structure does not scale. Separate folders for Views, Controllers, and often ViewModels when all three are tightly coupled to one another. You must bounce between all three folders anytime you need to add or change a feature. With Razor Pages, your `PageModel` (Controller + ViewModel) are in the same folder as your View. You hit F7 in Visual Studio to toggle between them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change "better" to "simpler". The word "better" seems too subjective.

Rick Anderson added 2 commits July 25, 2018 18:16
@Rick-Anderson
Copy link
Contributor Author

@scottsauber would like your comments and review.

@scottsauber
Copy link
Contributor

Definitely! I appreciate the opportunity to review this. I’ll review it tonight after work if that’s ok?

@scottsauber
Copy link
Contributor

scottsauber commented Aug 9, 2018

Really sorry... I'll look tonight. Got caught up with other things last night.

Edit: My plan was to just pull this branch down and run DocFX to view it... is that the best way since I don't have access to that URL?

@Rick-Anderson
Copy link
Contributor Author

@scottsauber no hurry-next week fine

@scottaddie
Copy link
Member

scottaddie commented Aug 9, 2018

@scottsauber DocFX is the best way to preview your changes locally. With DocFX installed, you can run docfx --serve from the directory in which the docfx.json file lives.

|[Partial views](xref:mvc/views/partial)| x | x |
| Filters | [MVC](xref:mvc/controllers/filters) | [RP](razor-pages/filter) |
|View Components | x | x|
|[Built in DI](xref:fundamentals/dependency-injection) | x | x | No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to think of some other ones to add here.

Some ideas if you think any of these are worth adding:

  • Validation/ModelState
  • Routing (although it is different in both)
  • Layouts
  • _ViewStart.cshtml and _ViewImports.cshtml
  • Tag Helpers

Ultimately... Razor Pages is just some sugar on top of MVC if that's part of the intent to drive home with this table.

* The app can authorize an entire folder and require the user to be an Administrator to get to any subfolder of */Administrator*. The code to do this is more straightforward than using multiple Controllers for Administrator features.
* **More secure by default.** Razor Pages provides:
* [AntiForgeryToken validation](xref:razor-pages/index#xsrfcsrf-and-razor-pages) by default.
* You opt-in to the properties that need to be model bound via `[BindProperty]`. `[BindProperty]` prevents over-posting attacks.
Copy link
Contributor

@scottsauber scottsauber Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe provide a link to over posting attacks in case someone reading has no clue what this is. OWASP uses the term Mass Assignment although their example is in Spring + Java. Maybe we should use over-posting/mass assignment since those terms are used interchangeably.

Not sure if you want to link to external blogs or not but Hanselman has a good example here that's MVC specific: https://www.hanselman.com/blog/ASPNETOverpostingMassAssignmentModelBindingSecurity.aspx and Scott Allen has a good one too: https://odetocode.com/Blogs/scott/archive/2012/03/11/complete-guide-to-mass-assignment-in-asp-net-mvc.aspx


By [Scott Sauber](https://twitter.com/scottsauber) and [Rick Anderson](https://twitter.com/RickAndMSFT)

Razor Pages is a feature of the ASP.NET Core MVC framework. Razor Pages follows the [MVVM](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm) pattern. Developers and teams familiar with ASP.NET MVC development:
Copy link
Contributor

@scottsauber scottsauber Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this kinda reads a little funny that it says it's a feature of the MVC framework, then immediately says it follows MVVM which someone might be like "what? how can it be a feature of MVC if it follows a (slightly) different pattern in MVVM?" Just kinda the unfortunate naming of MVC than anything else though Not sure I have any better wording here or specific suggestions, other than maybe moving that MVVM line to another part?

@scottsauber
Copy link
Contributor

scottsauber commented Aug 10, 2018

I think it should definitely be mentioned somewhere on this doc that the guidance for Razor Pages is to be used with server-rendered HTML scenarios and use MVC if you're strictly building API's for say a front-end like React, Angular, Vue, etc. Someone who's building just API's should not use Razor Pages. Part of the reasoning that could be mentioned is if you use Razor Pages for API's, you essentially have a useless .cshtml file laying around, because there's no View being served up.

You can send/receive JSON from Razor Pages, but if you do that, that API endpoint should probably be tightly associated with a page (like say a cascade dropdown scenario). Otherwise, a more generic API/RESTful API makes more sense to be in a Controller.

I think this is backed up by the guidance provided in the templates where the API template uses MVC. Along the same lines, the Web Application template with Razor Pages uses Controllers for things where there's no UI/Page to show (like Log Out).

Maybe there could be "When To Use Razor Pages" and "When To Use MVC" sections? Although "When To Use" may be a little strong. "Scenarios that lend themselves to Razor Pages"/MVC...? Wording is hard. 😆

@scottsauber
Copy link
Contributor

scottsauber commented Aug 10, 2018

That's all for my suggestions. Hope at least some of them are useful.

Again - I really appreciate that you reached out to have me take a look at this @Rick-Anderson!


MVC uses separate folders for Views, Controllers, and ViewModels when all three are tightly coupled. You must bounce between all three folders when you need to add/change/debug a feature. With Razor Pages, the `PageModel` (Controller + ViewModel) is in the same folder as the View. ~Hit F7 in Visual Studio to toggle between them.~

Razor Pages follow the [Single responsibility principle](https://wikipedia.org/wiki/Single_responsibility_principle), while MVC does not.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Razor Pages follow the Single responsibility principle, while MVC does not.

I'd remove this.

MVC uses separate folders for Views, Controllers, and ViewModels when all three are tightly coupled. You must bounce between all three folders when you need to add/change/debug a feature. With Razor Pages, the `PageModel` (Controller + ViewModel) is in the same folder as the View. ~Hit F7 in Visual Studio to toggle between them.~

Razor Pages follow the [Single responsibility principle](https://wikipedia.org/wiki/Single_responsibility_principle), while MVC does not.
* **Unit Testing is easier.** With a Controller, you might have many Actions and some of the dependencies injected that are related to only one or two Actions. When unit testing a single Action, the dependencies must be mocked or passed in as null. With Razor Pages, the dependencies you inject in are 100% related to GET, POST, PUT, etc actions in the `PageModel`. Unnecessary dependency injection in MVC testing can be mitigated with the [Builder pattern](https://visualstudiomagazine.com/articles/2012/07/16/the-builder-pattern-in-net.aspx).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I would say this.

@Rick-Anderson
Copy link
Contributor Author

@davidfowl removed

  • Unit Testing is easier. With a Controller, you might have many Actions and some of the dependencies injected that are related to only one or two Actions. When unit testing a single Action, the dependencies must be mocked or passed in as null. With Razor Pages, the dependencies you inject in are 100% related to GET, POST, PUT, etc actions in the PageModel. Unnecessary dependency injection in MVC testing can be mitigated with the Builder pattern.

Can we say anything about that? Unit testing typically doesn't require as many mocks?


By [Scott Sauber](https://twitter.com/scottsauber) and [Rick Anderson](https://twitter.com/RickAndMSFT)

Razor Pages is a feature of the ASP.NET Core framework. Razor Pages follows the [MVVM](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm) pattern. Developers and teams familiar with ASP.NET MVC development:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use the following relative URL instead: /xamarin/xamarin-forms/enterprise-application-patterns/mvvm
  • ASP.NET MVC --> ASP.NET Core MVC


Razor Pages can be added to controller/view projects. The controller-view approach to MVC may be preferred by larger teams.

This article compares Razor Pages to controller/views for creating server rendered HTML. ASP.NET Core MVC controllers are used to build Web APIs. For example, a front-end like React, Angular, Vue, etc. would use ASP.NET MVC controllers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Hyphenate "server rendered"
  • Web APIs --> web APIs
  • ASP.NET MVC --> ASP.NET Core MVC

Rick Anderson and others added 2 commits September 25, 2018 16:33
@Rick-Anderson Rick-Anderson mentioned this pull request Nov 9, 2018
@scottaddie scottaddie changed the title RP vs MVC [WIP] RP vs MVC Dec 12, 2018
@scottaddie scottaddie removed the WIP label Dec 12, 2018
@Rick-Anderson
Copy link
Contributor Author

@danroth27 can you schedule time to complete this before end of FY? cc @tdykstra

@Rick-Anderson
Copy link
Contributor Author

@danroth27 can I close this PR and turn it into a blog by me? It would start out saying this is not official, but my opinion.

@danroth27
Copy link
Member

@Rick-Anderson Sure.

@Rick-Anderson
Copy link
Contributor Author

@scottsauber closing PR and moved to https://github.com/Rick-Anderson/RP-vs-MVC where we can be more opinionated.

@Rick-Anderson Rick-Anderson deleted the RP-vs-MVC/ra branch October 17, 2019 01:36
@scottsauber
Copy link
Contributor

@Rick-Anderson - sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants