问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/3578045/mixing-webforms-and-mvc-what-should-i-do-with-the-masterpage