9

I use blazor server side whit visual studio 2022. I use MainLayout.razor as the default layout. but I dont understand what is _layout.cshtml in Pages folder and Pages\Shared folder use? Thanks.

2
  • 1
    The _layout.cshtml is for ASP.NET MVC pages I think you don't need it for Blazor Commented May 27, 2022 at 22:04
  • When you mean "in Areas\Identity\Pages\Shared" you should spell that out. Commented May 28, 2022 at 8:43

2 Answers 2

4

In .Net 6 Blazor project

MainLayout.razor :

  • MainLayout is used for default layout which is render in all blazor component, if we want to render other layout in some pages that time we need to specify layout.

  • MainLayout is used for set common content in single page

    @layout EmptyLayout 

as above we need to declare layout for specific components.

_Layout.cshtml:

  • Lower version of .Net 6 in Blazor we have _host.cshtml file in blazor project but in .Net 6 we have _layout and _host both file , as below _layout file is call from _host.
  • Here is the code of _host.cshml
@namespace admin_app.Pages @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @{ Layout = "_Layout"; } <component type="typeof(App)" render-mode="Server" /> 
  • Lower version of .Net6 these content set inside _host file, now _layout file is used for declare common css, js and common content which are used in all whole website.
  • We also create javascript function inside our _layout file.
Sign up to request clarification or add additional context in comments.

Comments

2
  • _Layout.cshtml renders <html>, <head>, and <body>.
  • MainLayout.razor renders extra <body> content.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.