Pages

Wednesday, May 25, 2011

Coexisting WebForms and MVC

In addition to my previous post on the topic, here are the steps to take if MVC is to be added to an existing WebForms project:

  • Add references to
    • System.Web.Routing
    • System.Web.Abstractions
    • System.Web.Mvc
    • System.Web.Helpers
  • Add directories to the existing site root
    • Controllers
    • Views
    • Content (optional) for images and CSS
    • Scripts (optional) for JavaScript
  • Update web.config to load the above assemblies at runtime and register the UrlRoutingModule
  • Set route(s)

If you decide to keep the default route then either copy (from a blank Mvc project) or create a new HomeController and the view(s) for the actions. The best way is to do the above tasks manually and then copy the content, like controllers, views, and scripts, from a starter Mvc site.

Web.config

These are the settings to add to web.config:

    <compilation debug="true" targetFramework="4.0">
<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>
</compilation>

and also

  <system.web>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>

 

No comments: