Practical ASP.NET

Nesting Layouts in ASP.NET MVC

If you've got a site that contains subsites and want to visually distinguish between those subsites (while still maintaining your site's visual integrity), consider nesting layouts within layouts.

It's worth remembering that the layout View you use in your site really is just a View: Pretty much anything you can do in a View, you can do in your layout View, including setting the Layout property. This can be useful if you have a site that's made up of several subsites because it lets you simplify the process of giving all of your subsites a distinct look-and-feel while maintaining an overall look-and-feel for the whole site.

To exploit this feature, first create a Layout for your whole site, leaving space in the View for your subsites to supply their HTML. Add to this view any site-wide stylesheets and script libraries that you use everywhere (the jQuery library would be a good choice, for example). This will probably be a pretty minimal View consisting, perhaps, only of the company logo, contact information and legal disclaimers. This View probably will just have a single RenderBody method and not use RenderSection.

Then create layout Views for each of your subsites, leaving space in those Views for the actual content that the Views called directly from Action methods will supply. In these subsite's layout Views, set their Layout property to your site-wide view:

  Layout = "~/Views/Shared/_Master.cshtml"
End Code

This is where you can put any stylesheets that tailor the subsite's appearance (or, really, any other resource used only in the subsite). This View is more likely to use RenderSection in order to provide more options for placing the variety of content required by your subsite's Views. Of course, if you've used RenderSection in your full-site layout, these are the Views where you'll define those sections.

There's another benefit in using this technique: When you want to change the appearance of a subsite you can do that in the subsite's layout View, knowing that you won't be doing harm to some other subsite's appearance.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus


  • VS Code Now Has Apple Silicon Builds for Native Mac Development

    Goodbye Rosetta, hello M1. Visual Studio Code has been updated with new builds that let it run natively on machines with Apple Silicon (M1), the company's own ARM64 chips.

  • Visual Studio 2019 for Mac v8.9 Ships with .NET 6 Preview 1 Support

    During its Ignite 2021 online event for IT pros and developers this week, Microsoft shipped Visual Studio 2019 for Mac v8.9, arriving with out-of-the-box support for .NET 6 Preview 1, which the company also released recently.

  • Analyst: TypeScript Now Firmly in Top 10 Echelon (Ruby, Not So Much)

    RedMonk analyst Stephen O'Grady believes TypeScript has achieved the rare feat of firmly ensconcing itself into the top 10 echelon of his ranking, now questioning how high it might go.

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

Upcoming Events