Adding MVC capabilities to an existing Web Forms project

If you want to add MVC capabilities to an existing Web Forms project, there are no automatic way to do it.  I wish the process was as simple as right-clicking on the Web Forms project and selecting something like “Add MVC Capabilities…” but no, it’s a manual process.

Before starting, make sure you backup your Web Forms project (just in case)  ;-)

This process work for VS 2010 Beta projects and I haven’t tested it on VS 2008 projects.

First, you need to add 3 references to your Web Forms project:

  • System.Web.Abstractions
  • System.Web.Mvc
  • System.Web.Routing

BTW, watch for the DLL versions number (MVC 1 & 2) an pick version 2.

For the next steps, the easiest way is to create a new MVC project and copy the required elements.

From the MVC project, open the Web.config file.  You’ll need all the MVC related configuration.  The config stuff you need is in the following sections:

  • Compilation
  • Pages
  • HttpHandlers
  • System.WebServer
  • Runtime

Copy these sections but make sure you don’t overwrite some configuration that’s already in your Web Forms Web.confg file!

Next, open the Global.asax file and copy the RegisterRoutes method and the call in the Application_Start.  Make sure to also add these using:

  • System.Web.Mvc
  • System.Web.Routing

Next, copy the Web.config file located in the Views folder. 

The project should now compile.

The last step is to tell Visual Studio that the project has now MVC capabilities.  To do that, you need to edit the project file and add the MVC project type GUID to the ProjectTypeGuid section.  If you don’t do it, you won’t be able to use the Visual Studio tooling to add controller and views.  In the MVC solution, right click on the MVC project and select Unload.  Now right click on the project and select Edit.  Locate the ProjectTypeGuids section and copy the first GUID.  As of VS 2010 Beta 2, the GUID is {F85E285D-A4E0-4152-9332-AB1D724D3325}.  Unload and edit the Web Forms project file and copy the GUID.  Make sure to leave no space.  Alternatively, you can use good old Notepad to edit the project file.

Bookmark and Share

1 Comment

Comments have been disabled for this content.