32

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.

1
  • Did you find a way to do this from the other way. I.E MVC > Webforms especially regarding html helpers in the MVC masterpage.
    – Damien
    Sep 18, 2009 at 15:15

3 Answers 3

27

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.

9
  • 1
    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. May 8, 2009 at 20:35
  • 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
    May 8, 2009 at 21:02
  • This works, but do you know a way around the way WebForms use the top level html form tag? stackoverflow.com/questions/1031252
    – Keith
    Jun 23, 2009 at 8:11
  • 1
    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
    Aug 7, 2009 at 17:05
  • 2
    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
    Dec 15, 2009 at 20:11
8

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.

1
  • But we cannot use html.actionlink :(
    – Tassadaque
    Dec 7, 2010 at 7:28
0

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?

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.