Chapter 3. Organizing Razor Templates

As the size of a website grows, so does the task of keeping everything organized. And, as any seasoned web developer will tell you: the bigger your site, the more difficult it becomes to maintain. Luckily, there are plenty of techniques developers can employ to keep websites from growing unmanageable. This chapter will introduce a number of Razor features that help make website organization and management a breeze.

Layouts

Now that our blog site has more than one page, you can begin to see that there is a bit of redundant markup between them. It is generally a good thing for all pages on your website to look the same; that is, they should look like they belong to the same site.

Thus far, the pages have maintained a consistent look and feel by duplicating the same markup on every new page, changing only the main content section. Not only is duplicating text in this way inefficient, it quickly becomes practically impossible to manage. For example, consider how much work it would take to add a simple CSS stylesheet to a site with a dozen or more individually maintained pages.

The problem of maintaining a consistent look and feel throughout an entire website is certainly not limited to WebMatrix and ASP.NET MVC sites. In fact, all good web frameworks must address this problem. Most address it by introducing the concept of a “layout.” When using layouts, a single page acts as a template for all other pages to use, defining the site-wide page layout and ...

Get Programming Razor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.