0
votes

I'm building a website using the new asp.net core 2.0 with Razor pages. I had trouble insert html blocks for reuse (for example a menu). I asked here and the answer I got was to use @{Html.RenderPartial

Then, when I used it I still got an error. To solve the error I had to remove the @page from the .cshtml file that I wanted to insert to the page. The reason, as I got answer here is because @{Html.RenderPartial is part of MVC, therefore it's a partial view and can't use the @page at the top of the page.

But, here's my confusion: in the documentation of Microsoft, they say very clear that now with the new asp.net core 2.0 you should put @page in every page (.cshtml file) you create.

So, now the question is: what's the proper way to insert html blocks for reuse in the new asp.net core 2.0, and if there's a proper way?

Because it seems that you either use the MVC, and have to remove @page. But Microsoft says you have to put the @page. So I'm confusing.

This is what they write: ". @page must be the first Razor directive on a page. "

from this page: https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/index?tabs=visual-studio

Also, the new ASP.NET core 2.0 is MVVM. So why do I have to use MVC in order to reuse html blocks?

Thanks

1

1 Answers

2
votes

Razor Pages is built on top of the MVC framework and reuses a lot of MVC primitives. Therefore a lot of what works in MVC also works in Razor Pages (but not everything).

A Razor Page is an endpoint. Therefore it needs the @page directive. A partial is not an endpoint. It's a reusable component. That's why it should not have an @page directive.

Since you asked the question, ASP.NET Core 2.1 has been released which includes the Partial taghelper. This is now the recommended way to include partials in Razor Pages (and ASP.NET MVC Core, for that matter).

Razor Pages is not MVVM. It's actually an implementation of the Page Controller pattern. Where the confusion might arise is with the PageModel. This is a hybrid of the MVC controller and a View Model in that it includes action methods (that can return IActionResult) and its public properties are exposed to the Razor Content pages as properties of the model.