开发者

Can WebForms and MVC live in a single project?

I'd rather be doing MVC as the regular WebForms development eats m开发者_StackOverflow社区y soul.

However, I've been given a fairly sizable WebForms project to add a bunch of features to.

Is there anyway for WebForms and ASP.NET MVC to coexist in single project or even jointly handle the website? I know I am asking for a hack.


Here goes...

  1. Make sure your project is targeting .NET 3.5
  2. Add references (and make sure they copy to local) the big 3 assemblies for MVC (System.Web.Mvc, .Abstractions, .Routing)
  3. Add a Global.asax if you don't already have one in your project.
  4. Go to Global.asax code behind and add the necessary code to make it look just like an MVC Global.asax (I'm not going to type it out, just create a new MVC project and look at the Global.asax and add the things your current Global.asax code behind is lacking)
  5. Create the top level folders for "Controllers" & "Views"
  6. Create sub-folders of "Views" for your corresponding controllers (ex. Views -> Home)
  7. When you add .aspx files to you view folders be sure to change the base class from System.Web.UI to System.Web.ViewPage and remove the tags
  8. Start a new blank MVC project and do a find for "UrlRoutingModule" in the web.config and add those refrences to your WebForms web.config in the same places
  9. Check the system.web -> pages -> namespaces node of the new MVC project and add the MVC specific ones to your WebForms web.config

This is a very rough explanation but you asked for the cliff notes. Moral of the story is to follow along with a new/blank mvc project and add the necessary entries in web.config, reference the necessary assemblies, and create the correct file structure for the mvc pattern.


I haven't done it - but it appears to be possible according to this Chad Myer's blog post. It's a bit old. http://www.chadmyers.com/Blog/archive/2007/11/30/asp.net-webforms-and-mvc-in-the-same-project.aspx


This is what I did to add MVC5 to an existing Web Forms project

  1. Create a new ASP.NET MVC project. You can use it to copy info to your existing project.

  2. In your existing project, use nuget to add the references. This will also update your Web.config and add the jQuery files. Copy this in your Package Manager Console

    Install-Package -Id "Microsoft.AspNet.Mvc" -Version 5.2.2" -ProjectName ExistingWebApp      
    Install-Package -Id "jQuery" -Version "1.10.2" -ProjectName ExistingWebApp
    Install-Package -Id "jQuery.Validation" -Version "1.11.1" -ProjectName ExistingWebApp
    Install-Package -Id "Microsoft.AspNet.Web.Optimization" -Version "1.1.3" -ProjectName ExistingWebApp
    Install-Package -Id "Microsoft.jQuery.Unobtrusive.Validation" -Version "3.2.2" -ProjectName ExistingWebApp
    Install-Package -Id "Modernizr" -Version "2.6.2" -ProjectName ExistingWebApp
  1. Copy BundleConfig, Routeconfig, FilterConfig and any others you need to App_Start

  2. Add the register lines from Application_Start to Global.asax. You can get them from your new project.

  3. Add these keys to the <appsettings> of your Web.config

    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    
  4. Add a reference to the Microsoft.CSharp assembly

  5. Add the Views and Controllers folder

  6. Now you can start adding views and controllers as you would do in any Mvc project


If you have room for another book I highly recommend Steve Sanderson's book http://bit.ly/1W03Tv (I'm not a mole...it's just helped me a ton). He has a chapter in there about "Combining MVC and WebForms".

I thought about summarizing the process out here but it's a bit lengthy. Give it a look if you get a chance.


Now in 2011 we have .NET 4.0 and is fairly easy to combine them. Just start a new MVC3 project, add a HomeController and a view (i.e. Home/Index.cshtml), then add a new folder, let's say /Admin, then add a web form inside it like /Admin/Default.aspx... that's it, run it, the home page will use MVC, the admin section will use web forms.


Also - in order to add the context menus, add the "MVC" project type ({E53F8FEA-EAE0-44A6-8774-FFD645390401}) to the project file as such

<ProjectTypeGuids>{E53F8FEA-EAE0-44A6-8774-FFD645390401};{...};{...}</ProjectTypeGuids>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜