How to add .aspx pages to existing MVC 4 project?

2020-02-17 07:15发布

问题:

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. 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!

回答1:

The solution:

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.

So what I did:

  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")
    

and in MasterPage.Master like this:

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

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.

Sources I used:

  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.