MVC Layout VS MVC Master Page
Asked Answered
P

2

19

I'm starting learning MVC4. I came across the two possibilities of creating a View based on a Razor template or a Master Page.

I would like to understand the practical differences between the two.

For now, I can see that if I create a View using a Master Page, I can override several sections. For example, if my Master defines a "left column" placeholder and a "body" placeholder I can not only define the body for a specific View, but I can also render contents in the "left column" section for example to display controls that are bound to the context in which the page is (from a search box to a stock quote viewer). Also, Master Pages cannot be defined by making use of Razor templates, which are much less verbose than other syntax (partially wrong: someone managed to hack this aspect).

With Razor Layouts, I can only define one contiguous block of the page that can be overridden by specific View, and I should use multiple layouts (breaking DRY) for little changes in other parts of the page. Is my previous statement correct or am I missing something?

Obviously I can render contents in any part of the page by making good use of jQuery, but that's another matter

Plywood answered 10/11, 2012 at 12:35 Comment(1)
In a Razor Layout you can definie multiple sections. You should read this article about how layouts and sections work in Razor.Tuff
N
27

You could use sections with Razor. Scott Gu blogged about them here: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

In your Layout you could define as many sections as you wish:

<div id="leftMenu">
    @RenderSection("LeftMenu", required: false)
</div>

which you could override in your views:

@section LeftMenu {
    <div>... here comes the left menu for this view ...</div>
}

You could also test whether a section has been defined in a view and if not provide some default content:

@if (IsSectionDefined("LeftMenu")) { 
    @RenderSection("LeftMenu")
}
else { 
    <div>Some default left menu</div>
}
Nahshon answered 10/11, 2012 at 13:7 Comment(0)
G
12

Starting with MVC3, the razor view engine was introduced. At a high level, a view engine is basically what takes the view and renders the necessary HTML. Razor uses the _layout.cshtml file and it's own templating system that is similar to master pages. However, MVC3 and 4 have another view engine called WebForms View Engine and this does use master pages. If you look at MVC1 and MVC2 tutorials respectively, you'll see master pages. Prior to MVC3 there was only WebForms View Engine.

Now in terms of functionality, both are similar. Master pages allow you to define content place holders while Razor allows you to define sections. The one major difference between the two would be in how the page is rendered. Master pages render the page outside in, meaning first the master page, then the content place holders. Razor is, I believe, recursive and starts with the innermost sections and works it's way back out.

Check out this blog post for more information on razor layouts: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

In terms of which one is preferred, both view engines exist; but if you want to utilize the razor syntax - which I highly recommend - then you have to use the layout system. Razor doesn't allow you to use master pages.

Gymnasiast answered 17/4, 2013 at 0:40 Comment(1)
Thank you for the detailed answer.Disadvantageous

© 2022 - 2024 — McMap. All rights reserved.