With previous versions of Blazor all files were cshtml pages and were able to use _layout similar to MVC projects - all was well.
But now in the new .NET Core 3.0 release Blazor template switched to *.razor files which are razor components (not razor pages). And the layout is now Shared/MainLayout.razor and is applied via routing in App.razor file:
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
So this creates confusion. We're still able to add razor pages (.cshtml files) to the project but they do not get the layout applied. It would be a pain to create and maintain 2 separate identical layouts, 1 for razor pages and 1 for razor components. I was unable to find any guidance for this.
Is there any way to apply razor component layout (Shared/MainLayout.razor) to razor pages (.cshtml files) inside the same project? Of if not, what is the recommended approach?
in the head of your _Layout page add
<base href="~/" />
in the bottom of body add
<script src="_framework/blazor.server.js"></script>
then in your _Host.cshtml file add
@{
Layout = "_Layout"; //your Layout name
}
for more information follow this link
I currently have the same problem and it's really annoying. Would also be interested in a solution for this. I can't even load the layout with
@{
Layout = "shared/MainLayout.razor";
}
because it expects a file with the name MainLayout.razor.cshtml then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With