Question

As you can see below my MVC site contain _ViewStart.cshtml file in the root of my Views folder that use the _Layout.cshtml from Shared folder for sharing the layout of Header and Footer. But the Main Content is different for each page.

enter image description here

But now I have same Main Content layout for my Category folder pages. So my question is, how can I use another updated _Layout and _ViewStart files for Category folder pages that also use the Header and Footer by my _Layout.cshtml and _ViewStart.cshtml files or I need to specify the same Section for each page in Category folder? I want to use something like _ViewStart.cshtml file in the Root of Category folder that can share Main Content layout for Category pages.

Perhaps it seems a bit confusing, but I hope you can understand what I'm trying to ask.

Was it helpful?

Solution

You make a _CategoryLayout.cshtml within the category folder and put the following in the category page(s):

@{ Layout = "~/Views/Category/_CategoryLayout.cshtml"; }

Then you can use this layout to add footers and headers to your page, in turn this layout page (_CategoryLayout) can have its own layout. So you can add the following code in that layout:

@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

This way it makes use of both layouts. You can ofcourse get more out of it by applying the right Sections, don't forget to fill a section in the page itself you need to define the section in every layer of layout till you display it. So if you for example have a CSS section which you want to use in the category page, you also would need to define it in the categorylayout.

Structure of nested layouts

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top