Reputation: 11680
Suppose I have a .net 5.0 website that has both Razor pages and MVC pages.
That is, in Startup.Configure, I have:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
});
And I have some pages like:
And others like:
Is there a way to have them use the same layout?
It seems simple enough to create a Razor layout in /Pages/Shared/_Layout.cshtml and another in /Views/Shared/_Layout.cshtml, but if you want to have identical layouts for both types of pages, this seems a violation of the Don't Repeat Yourself principle.
Is there a way of sharing a single layout between both types of page?
Just as a followup - copying /Pages/Shared/_Layout.cshtml to /Views/Shared/_Layout.cshtml (and making sure /Views/_ViewStart.cshtml is set) works, except that the asp-page helpers don't work. Setting the URLs with plain href= works fine.
Upvotes: 8
Views: 1533
Reputation: 18139
Yes,it is possible.For example,you have a layout in /Pages/Shared/_Layout.cshtml
.And you want to set the layout in views with it.
Change Views/Shared/_ViewStart.cshtml
like this:
@{
Layout = "~/Pages/Shared/_Layout.cshtml";
}
So that both the layouts in Pages and Views will be /Pages/Shared/_Layout.cshtml
.
Upvotes: 10