How would you sprinkle-in ASP.Net MVC into an exis

2019-01-16 04:46发布

I have a legacy (haha) ASP.Net Webforms Web Site Project in Visual Studio 2008 SP1, that I would like to gradually introduce some MVC functionality into.

Most of the information I can locate on how to integrate ASP.Net MVC with WebForms seems to assume the use of a Web Application Project. However, it seems impossible to find information about how to retrofit an existing ASP.net Web Site Project with the ASP.Net MVC features.

I've reviewed Scott Hanselman's post and Chapter 13 of his upcoming book, both of which assume the Web Application Project type.

Is this possible? Does anyone have a how-to on this?

8条回答
forever°为你锁心
2楼-- · 2019-01-16 05:07

The Microsoft .NET 4.0 exam on Web development (70-519) has almost this exact question in the prep materials. The answer, according to Microsoft then, is:

  1. Convert the web forms website into a web application (i.e. webapp project).
  2. Add references to "the ASP.NET MVC 2 assemblies" in the webapp's config file.

This info is in paid-for materials my employer purchased so there is not necessarily a web page stating it this clearly to which I could link.

查看更多
一夜七次
3楼-- · 2019-01-16 05:16

I had a pretty big ASP.NET web site (not a web application) and wanted to add MVC3 to it. I had no option to change the type of the project so I had to go with the web site (asp.net 4.0).

I use a separate MVC project but not as it's own web application but as an assembly within my old web site.

Here's a rundown of what I did:

  • I created a new MVC3 web application in Visual Studio 2010, I used the empty template and the Razor view engine.
  • I added it to the solution with my existing web site.
  • I changed the output path for the assembly from the local bin directory to the bin directory of my web site.
  • I removed the 'Content' and 'Scripts' folders from the MVC app. Both content and scripts are already part of my web site and I can reference these from MVC 'pages' as well.
  • I removed the Global.asax.* files from the project. I am using the Global.asax in the web site.
  • I copied the 'Views' folder and subfolders over to the web site. Because these are actual files and not part of the assembly, they have to exist within the web site not the project that builds the MVC assembly.
  • At this point I could have deleted the 'Views' folder from the MVC project but it is only in this project type that I get Visual Studio support for adding a new view. So I sometimes create a view here and then move it over to the web site. When editing the cshtml files in my web site, I still get full Intellisense.
  • Added Routing to the web site. I just copied over some code from an MVC global.asax in the global.asax of my web site. We need some usings:

    using System.Web.Mvc;
    using System.Web.Routing;
    

In Application_Start we need:

    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

Then add the usual routing methods:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // your routes
    }

Next add a few things to the web.config of your web site. In system.web under compilation we need the following assemblies:

     <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>

Initially I also added some MVC namespace to web.config but it seems to work fine without them.

You do now create new routes in Global.asax of the web site, then add a corresponding controller in the MVC project and then back to the web site to add a view for it. So you logic is all in a assembly while the views and the routing is defined in the web site.

You can still debug into the MVC controllers by setting break points there, but you debug by starting the web site.

If you use the suggested default route of MVC:

        routes.MapRoute("Default", "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

a call to www.mysite.com will serve up the content of the home controller/view not your old default.aspx home page, so I just don't use such a route. If you routes conflict with existing physical folders and files, use constraints with regular expressions on the routes to exclude such conflicts.

Even though I use master pages in the web site, the actual html for the common page parts is created by code in another assembly. I could just call the same methods from my _ViewStart.cshtml or my base controller.

So far I have not seen any real negative about this approach.

查看更多
登录 后发表回答