Sunday, May 18, 2008

Nested Master Pages with the MVC Framework

If you install the MVC Framework CTP2 you may notice that it doesn't come with a nested master page template, but don't worry it very easy to create your own from the existing Web Forms template. Here's how:

First add a standard (web forms) nested master page:


Next, in the code behind file 'myNested.master.cs' inherit from System.Web.Mvc.ViewMasterPage:


Then you are ready to go, just add a content placeholder:



Anonymous said...

following your instruction but i cannot get it work. I inherit the nested masterpage from system.web.mvc.viewmasterpage, in the code behind, but i can not do Html.ActionLink on the html part of the master page.

Mike Hadlow said...

Hi Anonymous,

Check out the master pages in the Suteki Shop code (my open source eCommerce application), to see how it's done.

Mr. Bluesummers said...

Yah, same here. I tried creating a nested master page (with codebehind like yours), but I'm getting parser errors:

The type 'System.Web.Mvc.ViewMasterPage' is ambiguous: it could come from assembly [my project] or from assembly [System.Web.Mvc].

Mr. Bluesummers said...

In fact, after downloading your code, your nested master pages are subject to the same error that Anonymous described- the HTML helper isn't found.

Mr. Bluesummers said...
This comment has been removed by the author.
Andy said...

It's piece of cake :)

Create main mvc master page, than create view and replace top line % Page ....


%@ Master Language="C#" MasterPageFile="~/Views/Shared/CMS.Master" Inherits="System.Web.Mvc.ViewMasterPage" %

this is going to be your second master page, insert your own place holders into asp:content tags

create view with second master page, done :)

Anonymous said...

I no see the difference between the difference between master page and the nested page in your sample.