如何将.aspx页添加到现有的MVC 4项目? [英] How to add .aspx pages to existing MVC 4 project?

查看:228
本文介绍了如何将.aspx页添加到现有的MVC 4项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have working ASP.NET MVC 4 project. I want to add to this MVC project 2 .aspx pages of another WebForms project. I have several questions:

  1. 我应该在哪里复制此.aspx文件,我应该如何 config 路线?
  2. 我应该如何告诉此.aspx页使用~/Shared/_Layout.chtml作为母版页?
  3. 另外,此 .aspx页使用 .ascx控件.我应该在哪里存放它们?
  4. 如何修改 web.config文件?
  1. Where should I copy this .aspx files and how should I config my routes?
  2. How should I tell this .aspx pages to use ~/Shared/_Layout.chtml as a master page?
  3. Additionally this .aspx pages uses .ascx controls. Where should I store them?
  4. How should I modify web.config file?

我查看了

I have looked at the links posted in this question, but they seemed to be outdated. A lot of the links explain how to add MVC to WebForms, but I am looking for all the way around.

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

Any useful links would be appreciated. Thanks!

推荐答案

解决方案:

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

As it turned out, adding .aspx pages to the existing MVC project is an even easier task to accomplish than adding mvc to .aspx. The most interesting thing for me was to find out that both webforms and MVC, in the scope of one project share one runtime on IIS.

那我做了什么:

  1. 将另一个项目的 .aspx 页面添加到我的 MVC 项目的
  2. 为我的网络表单页面创建了新的母版页(右键单击项目->添加->新项目->母版页)
  3. 为了使WF的 Master Page 和MVC的 _Layout.chtml 共享相同的 Master'View',我发现这篇很棒的文章,它使您可以调用类似于 .aspx页面
  4. 中的> @ Html.RenderPartial()方法
  5. 以下代码提供了有关如何在WebForms中实现 RenderPartial 方法的信息:

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

}

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

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

<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>

  • 由于我所有页面之间仅共享菜单",因此将其添加到部分视图中,然后在 _Layout.chtml

    @Html.Partial("_Menu")
    

  • 并在 MasterPage.Master 中像这样:

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

    仅此而已.结果,我的 _Layout.chtml MasterPage.Master 使用了相同的共享局部视图.而且,我可以通过在 .aspx 页面上导航来访问它们.如果路由系统遇到问题,可以在App_Start中将routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");添加到routeConfig中.

    That is all there is to it. As a result my _Layout.chtml and MasterPage.Master use the same shared partial views. And I can acess .aspx pages simply by navigating on them. If you have some issues with routing system, you can add routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); to your routeConfig in App_Start.

    我使用的来源:

    1. 将ASP NET MVC与WebForms组合在一起
    2. 混合Web表单和ASP.NET MVC
    3. MixingRazorViewsAndWebFormsMasterPages
    4. 如何在网络表单中包括部分视图
    1. Combine asp net mvc with webforms
    2. Mixing Web Forms and ASP.NET MVC
    3. MixingRazorViewsAndWebFormsMasterPages
    4. How to include a partial view inside a webform

    我希望以后会有所帮助.

    I hope it will help somebody later on.

    这篇关于如何将.aspx页添加到现有的MVC 4项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆