views:

1494

answers:

4

I am adding MVC to a project that has MANY legacy webform pages. This works fine. However, I currently have a separate masterpage for MVC and for the webforms. The two master pages produce essentially identical output. I'd really like to kill the webforms one and just use the MVC master page with all my pages and stay DRY.

Not being DRY has already bitten me a couple times when I forgot to change both.

I tried doing the obvious way and just pointing the webform content page's MasterPage attribute at the MVC masterpage. This throws an error saying the MVC masters only work with MVC views.

This seems like it would be a pretty common problem with mixed MVC and webform projects. My MVC master isn't doing anything with ViewData, so I don't see any reason the webforms couldn't use them.

+10  A: 

You can absolutely share the same master page. Your MVC master page must simply point to the WebForms masterpage via its MasterPageFile attribute. This applies your WebForms MasterPage styles to your MVC MasterPage.

I am using this setup in production.

The declaration on my MVC Master Page, pointing at the Web Forms Master Page:

<%@ Master Language="C#" MasterPageFile="~/MasterPage/Site.Master"
AutoEventWireup="true" Inherits="System.Web.Mvc.ViewMasterPage" %>

Works like a charm.

Peter J
Yeah, that makes sense. However I would really prefer to do it the other way around and use the MVC master for the legacy pages as it's much better designed.
Craig Quillen
I can't think of a way to force System.Web.Mvc.ViewMasterPage to allow child controls of type System.Web.UI.Page instead of System.Web.Mvc.ViewPage, but that is what you want in this scenario.
Peter J
This works, but do you know a way around the way WebForms use the top level html form tag? http://stackoverflow.com/questions/1031252
Keith
Unfortunately, my master page does not use that form tag - so I'll be no help on that other question.
Peter J
When I try this I get "Cannot find ContentPlaceHolder 'MainContent' in the master page '/Views/Shared/Site.Master'". My MVC master page contains only the 1 line you gave above, am I doing something wrong?
MrDustpan
The 1 line I gave above is only for the page declaration at the top of the master page. The other elements (ContentPlaceHolders) must also be present.
Peter J
As for the "Cannot find Content...", you will need to setup to be a child page with <asp:Content > tags.
Greg Ogle
Is there a way you can dynamically set the MasterPageFile property? In webforms you can do it in the codebehind, not sure how you'd do it in MVC.
Ryu
MVC can have codebehinds (they're simply not created by default). You can always use the "Inherits" property in your declaration, pointing to a .cs file.
Peter J
A: 

Not sure about your situation, but my WebForms master page contains a number of server controls and user controls (runat="server") the majority of which will not work when served up by the MVC stack.

I'm about to do a hybrid project, adding MVC into existing WebForms and plan on creating a new (duplicate style) master page.

Not ideal.

Matt Davis
System.Web.Mvc.ViewMasterPage does inherit from System.Web.UI.MasterPage
Matt Davis
A: 

In my webforms app, my master page inherits from "HLPUSD.SMART.SMARTMaster" which is just the namespace for our application and then the name of the webform class.

In my MVC project, the master page inherits from "System.Web.Mvc.ViewMasterPage"

Me thinks this has something to do with it?

Nick DeVore
+1  A: 

This blog post walks you through the necessary steps to share WebForm and MVC master pages with little or no duplication. It also includes a sample project you can download, and I found it quite helpful.

One hiccup I ran into was that I was using a LoginStatus control in my header. LoginStatus must be inside a form so I couldn't use it in my root master page (not wanting to end up with nested forms on all my MVC pages). But that was a pretty easy control to replace with a simple code block in my root master page.

Ashley Tate