Is it possible to share a masterpage between MVC a

2019-01-13 06:05发布

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.

3条回答
迷人小祖宗
2楼-- · 2019-01-13 06:20

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.

查看更多
贪生不怕死
3楼-- · 2019-01-13 06:27

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?

查看更多
老娘就宠你
4楼-- · 2019-01-13 06:35

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.

查看更多
登录 后发表回答