如何将.aspx页面添加到现有的MVC 4项目中?

13

我有一个正在使用ASP.NET MVC 4的项目。我想将另一个WebForms项目中的2个.aspx页面添加到这个MVC项目中。我有几个问题:

  1. 我应该将这些.aspx文件复制到哪里,如何配置我的路由?
  2. 我应该如何告诉这些.aspx页面使用~/Shared/_Layout.chtml作为母版页
  3. 此外,这些.aspx页面使用.ascx控件。我应该把它们存储在哪里?
  4. 我应该如何修改web.config文件

我已经查看了links发布在this问题中的链接,但它们似乎已经过时了。很多链接都是解释如何将MVC添加到WebForms中,但我正在寻找相反的方式。

任何有用的链接都会被赞赏。谢谢!


Scott Hanselman的这篇文章http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx仍然有效,无论您将WebForm应用程序集成到MVC中还是反之,步骤都是相同的。在.aspx页面上使用Razor模板作为母版页是不起作用的,由于使用了不同的视图引擎,因此无法将它们组合在一起。 - Andreas
@Andreas 感谢您的评论。如果无法将它们合并,那么是否可以采用另一种方法来实现? - Shukhrat Raimov
1
最简单的方法是拥有一个ASP.NET WebFormViewEngine (.aspx)主页面和一个Razor模板基础布局(.chtml)。 - Andreas
1个回答

19

解决方案:

事实证明,将.aspx页面添加到现有的MVC项目中比将mvc添加到.aspx更容易实现。最有趣的是,我发现在同一个项目的范围内,WebForms和MVC共享IIS上的一个运行时。

所以我做了什么:

  1. Added .aspx pages from another project to the root of my MVC project
  2. Created new master page for my webforms pages(right click project->add->new item->Master Page)
  3. In order to make Master Page of WF and _Layout.chtml of MVC share the same Master 'View' I found this great article that allows you to call something similar to @Html.RenderPartial() method within .aspx page
  4. The following code provides information how to implement RenderPartial method in WebForms:

    public class WebFormController : Controller { }
    
    public static class WebFormMVCUtil
    {
    
        public static void RenderPartial( string partialName, object model )
        {
            //get a wrapper for the legacy WebForm context
            var httpCtx = new HttpContextWrapper( System.Web.HttpContext.Current );
    
            //create a mock route that points to the empty controller
            var rt = new RouteData();
            rt.Values.Add( "controller", "WebFormController" );
    
            //create a controller context for the route and http context
            var ctx = new ControllerContext( 
            new RequestContext( httpCtx, rt ), new WebFormController() );
    
            //find the partial view using the viewengine
            var view = ViewEngines.Engines.FindPartialView( ctx, partialName ).View;
    
            //create a view context and assign the model
            var vctx = new ViewContext( ctx, view, 
                new ViewDataDictionary { Model = model }, 
                new TempDataDictionary() );
    
            //render the partial view
            view.Render( vctx, System.Web.HttpContext.Current.Response.Output );
        }
    
    }
    

    Add it to codebehind.cs of your .aspx page. Then you can call it from the webforms like this:

    <% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
    
  5. Since I had only the 'menu' shared among all my pages, I added it to the partial View, then called it in _Layout.chtml

    @Html.Partial("_Menu")
    

并且在 MasterPage.Master 中像这样:

    <% WebFormMVCUtil.RenderPartial("_Menu", null ); %>

这就是全部内容了。因此,我的_Layout.chtmlMasterPage.Master使用相同的共享部分视图。我可以通过简单地在它们之间导航来访问.aspx页面。如果您在路由系统方面遇到问题,可以在App_Start中的routeConfig中添加routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
我使用的来源有:
  1. 将asp net mvc与webforms结合使用
  2. 混合Web Forms和ASP.NET MVC
  3. 混合RazorViewsAndWebFormsMasterPages
  4. 如何在Web表单中包含部分视图

我希望它以后能帮助某些人。


2
我在Web表单中调用GetModel()时遇到了问题。我看到的错误是“z.aspx不包含'GetModel'的定义,也没有接受'type 'z_aspx'的第一个参数的扩展方法可以找到(您是否缺少使用指令或程序集引用?)”。我找不到GetModel的定义在哪里。 - Echo Train

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接