views:

195

answers:

3

I'd rather be doing MVC as the regular WebForms development eats my 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.

+1  A: 

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

Chuck
Yeah, it's more than 2 years old and done with a preview. If someone could confirm whether this works with 1.0 bits, that would be great.
AngryHacker
According to Chad Meyers, it still works.
AngryHacker
A: 

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.

DM
Give me the reader's digest version.
AngryHacker
+1  A: 

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.

DM
Thanks. This is great.
AngryHacker
One more thing is needed to make it work. See the accepted answer to this question: http://stackoverflow.com/questions/1727309/webforms-and-asp-net-mvc-co-existance
AngryHacker