混合 WebForms 和 MVC:我应该如何使用 MasterPage?
我想开始将 WebForms 应用程序迁移到 MVC。这个过程将是渐进的,因此两个系统必须共存。
问题是:我是否应该有两个 MasterPage,一个用于 WebForms 页面,另一个用于 MVC 视图?有没有办法只拥有一个?
I want to start migrating a WebForms App to MVC. The process will be gradual, so both systems must co-exist.
The question is: Should I have two MasterPages, one for the WebForms pages and other for the MVC views? Is there a way to have only one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ASP.NET MVC 中,母版页应派生自 < code>System.Web.Mvc.ViewMasterPage 在来自
System.Web.UI.MasterPage
。如果在 MVC 中使用后者,您将无法访问任何帮助程序。尽管您可以在经典 Web 表单中使用ViewMasterPage
,因为它派生自MasterPage
(您将再次无法访问 Web 表单应用程序中的帮助程序,但谁在乎呢)。因此,要回答您的问题,是的,您可以拥有一个通用母版页,假设它派生自
ViewMasterPage
。话虽这么说,您可能无法完成这项工作,因为在 MVC 母版页中您将使用 HTML 帮助器来渲染部分视图,例如
Html.RenderPartial
,这在经典中没有多大意义WebForms 应用程序,反之亦然 在经典的 WebForms 应用程序中,您可能会使用一些服务器端控件,例如
或有一个form 标签(再次使用
runat="server"
)被 ViewState 等污染......这在 MVC 中几乎没有任何意义。所以我的建议是不要这样做。In ASP.NET MVC the master page should derive from
System.Web.Mvc.ViewMasterPage
while in classic WebForms fromSystem.Web.UI.MasterPage
. If in MVC you use the latter you won't have access to any helpers. Although you could useViewMasterPage
in classic webforms because it derives fromMasterPage
(once again you won't have access to helpers in the web forms application but who cares).So to answer your question, yes, you could have a common master page assuming it derives from
ViewMasterPage
.This being said you probably won't be able to make this work as in an MVC master page you would use HTML helpers to render partial views like
Html.RenderPartial
which doesn't make much sense in a classic WebForms application and vice versa in a classic WebForms application you would probably be using some server side controls like<asp:xxx runat="server" />
or have a singleform
tag (again withrunat="server"
) polluted with ViewState, etc... which hardly makes any sense in MVC. So my recommendation would be not to do like this.