Is it possible to share a masterpage between MVC and webforms?
Asked Answered
J

3

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.

Josephus answered 8/5, 2009 at 19:31 Comment(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.Cadge
C
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.

Craniotomy answered 8/5, 2009 at 19:33 Comment(9)
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.Josephus
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.Craniotomy
This works, but do you know a way around the way WebForms use the top level html form tag? stackoverflow.com/questions/1031252Shebeen
Unfortunately, my master page does not use that form tag - so I'll be no help on that other question.Craniotomy
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?Saval
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.Craniotomy
As for the "Cannot find Content...", you will need to setup to be a child page with <asp:Content > tags.Periderm
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.Koressa
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.Craniotomy
E
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.

Elbrus answered 11/5, 2010 at 21:8 Comment(1)
But we cannot use html.actionlink :(Ackack
M
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?

Moot answered 8/5, 2009 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.