混合 WebForms 和 MVC:我应该如何使用 MasterPage?

发布于 2024-09-16 10:58:33 字数 129 浏览 2 评论 0原文

我想开始将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-09-23 10:58:33

在 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 from System.Web.UI.MasterPage. If in MVC you use the latter you won't have access to any helpers. Although you could use ViewMasterPage in classic webforms because it derives from MasterPage (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 single form tag (again with runat="server") polluted with ViewState, etc... which hardly makes any sense in MVC. So my recommendation would be not to do like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文