Question

I'm working on ASP.NET MVC 4 application which has user part and admin panel. For the user part I use the default _Layout view but in my Views folder I have subfolder Admin where I plan to set all my views for the admin panel and I want a different layout for them, let's say - _AdminLayout. For now I use:

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

on top of each view in the Admin folder, but I wonder if there's a way to set _AdminLayout as default layout only for those views that are inside this folder and thus get rid of the explicit declaration for each view?

Was it helpful?

Solution

~/Views/Admin/_ViewStart.cshtml

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

You can place a _ViewStart in any of the controller view folders to override the parent one. Convention makes it look to the most local path first, then proceed to Shared.

As an aside, you may want to look in to using areas to keep it compartmentalized, but that's really up to you and how you want to structure your project.

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