Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Pages and Razor Views

Is it a good idea to mix Razor Pages with Razor Views in the same ASP.NET Core 2.0 app?

If I understand it correctly, the benefit of using Razor Pages is that they don't need controller actions. In my app, I have some pages that simply return a React app. I feel Razor Pages are perfect for this because all I need in the page is just a link to the bundle.js file.

Having said that, I do have a few pages where I need a controller and send a view model.

So, is it a good idea to have both pages and views or should I stick to one and not mix it together?

like image 655
Sam Avatar asked Aug 18 '17 04:08

Sam


1 Answers

Read recently about Razor Pages in docs and Rick Anderson(member of ASP.Core team) said here that:

You can mix controllers, views and Razor Pages. RP are unit testable (using code-behind). It's a good question. See https://github.com/aspnet/Mvc/issues/494#issuecomment-232534742 for motivation.

So you can mix. The only tiny issue, in my opinion, that I read from the docs that:

Note: The layout is in the Pages folder. Pages look for other views (layouts, templates, partials) hierarchically, starting in the same folder as the current page. This means that a layout in the Pages folder can be used from any Razor page under the Pages folder.

Which means that you cannot use the same layouts, templates, partials both for Razor Page and View out of the box, without modifying ASP conventions or writing some custom solution to use the same layouts, templates, partials both for Razor Page and View. Otherwise mix them looks perfectly fine.

like image 53
user2771704 Avatar answered Sep 30 '22 04:09

user2771704