9

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.

6 Answers 6

9

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.

1
2

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

1
  • 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. Nov 6, 2009 at 3:37
1

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

0

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.

0
0

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.

2
  • I tried creating a clean MVC 3 .Net 4.0 project and added a "foo" folder and a default.aspx page for that. I ran in debug mode and then navigated to /foo and /foo/default.aspx and received "resource could not be found". What else am I missing?
    – Jeff LaFay
    Aug 2, 2011 at 17:55
  • Do you have a controller and their related view for main MVC project? The Foo folder must have only the .aspx (and .aspx.cs) files, and those must have been created from choosing "New Web Form" item.
    – Nestor
    Aug 22, 2011 at 8:32
-1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.