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

舒赫拉特·莱莫夫(Shukhrat Raimov)

我有正在工作的ASP.NET MVC 4项目。我想将.aspx另一个WebForms项目的2添加到此MVC项目中。我有几个问题:

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

我查看了问题中发布链接,但它们似乎已过时。许多链接说明了如何将MVC添加到WebForms,但是我一直在寻找。

任何有用的链接将不胜感激。谢谢!

舒赫拉特·莱莫夫(Shukhrat Raimov)

解决方案:

事实证明,与将Mvc添加到.aspx相比,将.aspx添加到现有的MVC项目是一件更容易完成的任务。对我而言,最有趣的事情是发现在一个项目范围内,webforms和MVC在IIS上共享一个运行时。

所以我做了什么:

  1. 来自另一个项目的.aspx添加我的MVC项目根目录
  2. 为我的Web表单页面创建了新的母版页(右键单击项目->添加->新项目->母版页)
  3. 为了使WF的母版页MVC的_Layout.chtml共享相同的“母版”视图,我发现了这篇很棒的文章,它允许您.aspx页调用类似于@ Html.RenderPartial()方法的内容。
  4. 以下代码提供了有关如何在WebForms中实现RenderPartial方法的信息

    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 );
        }
    
    }
    

    将其添加到.aspx页的codebehind.cs。然后,您可以从网络表单中调用它,如下所示:

    <% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
    
  5. 由于我所有页面之间只有“菜单”共享,因此将其添加到部分视图中,然后在_Layout.chtml中将命名

    @Html.Partial("_Menu")
    

MasterPage.Master中是这样的:

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

这就是全部。结果,我的_Layout.chtmlMasterPage.Master使用相同的共享局部视图。我可以通过在.aspx页面上导航来访问它们。如果路由系统存在一些问题,可以routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");在App_Start中添加到routeConfig中。

我使用的来源:

  1. 将ASP.NET MVC与WebForms结合
  2. 混合使用Web窗体和ASP.NET MVC
  3. MixingRazorViewsAndWebFormsMasterPages
  4. 如何在Webform中包含局部视图

希望以后能对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将现有的Eclipse Java项目添加到git

如何将现有的Python文件导入/添加到PyCharm项目?

如何将pom.xml添加到现有的Eclipse项目中?

如何将Web API添加到现有的ASP.NET MVC(5)Web应用程序项目中?

如何将Web API添加到现有的ASP.NET MVC 4 Web应用程序项目中?

如何将Core Data添加到现有的Xcode 9 Swift 4 iOS 11项目中?

如何将监视模块添加到现有的Android Studio项目中?

如何将UI工具包(例如Get Shit Done)添加到现有的Rails项目中?

如何将MVC 4项目转换为AWS Lambda Function?

如何将现有项目添加到TFS

如何将Bootstrap 3 Datepicker添加到Angular 4项目

如何将现有的非git项目添加到bitbucket

将Angle 6添加到现有的MVC项目

如何将Vue.js添加到现有的ASP .NET Core 2.0 MVC项目中?

如何将eslint添加到现有的vue-cli项目中?

将Composer添加到现有的Codeigniter项目

如何将dbmate添加到现有项目?

如何将Angular项目添加到现有的.NET Core Web API项目中?

如何将MVC 5项目模板添加到VS 2012?

如何将Web API添加到现有的MVC Hottowel项目

如何将jQuery Mobile添加到现有的Worklight 6.2项目

如何将scalatest依赖项添加到现有的scala / lift项目中?

将Web Forms添加到现有的MVC项目

将WebAPI添加到现有的MVC项目中-获取404

将WebAPI添加到现有的aspnetcore MVC项目

如何将libgdx添加到现有的android studio项目中?

使用 .Net 4 将 MVC 项目添加到现有的 Web 应用程序

将 GeoDjango 添加到现有的 Django 项目

如何将打字稿添加到现有的 vue3 项目中?