是否可以在 MVC 和 webforms 之间共享母版页? [英] Is it possible to share a masterpage between MVC and webforms?

查看:26
本文介绍了是否可以在 MVC 和 webforms 之间共享母版页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 MVC 添加到一个拥有许多遗留 Web 表单页面的项目中.这工作正常.但是,我目前有一个单独的母版页用于 MVC 和 webforms.两个母版页产生基本相同的输出.我真的很想杀死 webforms 之一,只将 MVC 母版页与我的所有页面一起使用并保持干燥.

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.

我尝试了一种显而易见的方法,只是将 webform 内容页面的 MasterPage 属性指向 MVC 母版页.这会引发一个错误,指出 MVC 主控仅适用于 MVC 视图.

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.

这似乎是混合 MVC 和 webform 项目的一个非常普遍的问题.我的 MVC 主人没有对 ViewData 做任何事情,所以我看不出有任何理由让 webforms 不能使用它们.

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.

推荐答案

您可以完全共享同一个母版页.您的 MVC 母版页必须通过其 MasterPageFile 属性简单地指向 WebForms 母版页.这会将您的 WebForms MasterPage 样式应用到您的 MVC MasterPage.

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.

我的 MVC 母版页上的声明,指向 Web 窗体母版页:

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" %>

就像一个魅力.

这篇关于是否可以在 MVC 和 webforms 之间共享母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆