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:

@Code
  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 http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube