Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor .NET Core 3.0 - Can cshtml pages use MainLayout.razor

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?

like image 627
AlexVPerl Avatar asked Oct 15 '22 11:10

AlexVPerl


2 Answers

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

like image 190
Seyed Reza Dadrezaei Avatar answered Oct 20 '22 17:10

Seyed Reza Dadrezaei


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.

like image 42
d00d Avatar answered Oct 20 '22 18:10

d00d