Adding MVC to an existing ASP.NET Application

ASP.NET MVC Has added a very useful new programming model to developing websites in.NET. There is hype and debate all throughout the interwebs on how great MVC is. Andit all can be yours, with nothing but a ‘File->New’ in Visual Studio….unless youalready have an ASP.NET application. In which case, you need to do a bit more work.

  1. Add project references to:
    System.Web.Abstractions
    System.Web.Mvc
    System.Web.Routing
  2. Create /Controllers, /Views, and /Views/Shared folders in your project
  3. Update web.config
    <?xml version="1.0" encoding="utf-8" ?><configuration><system.web><pages><namespaces><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.Linq"/><add namespace="System.Collections.Generic"/></namespaces></pages><compilation><assemblies><add assembly="System.Core,Version=3.5.0.0,Culture=neutral,PublicKeyToken=B77A5C561934E089"/><add assembly="System.Web.Mvc,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35" /><add assembly="System.Web.Abstractions,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"/><add assembly="System.Web.Routing,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation><httpModules><add name="UrlRoutingModule"type="System.Web.Routing.UrlRoutingModule,System.Web.Routing,Version=3.5.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35" /></httpModules></system.web><system.webServer><validation validateIntegratedModeConfiguration="false"/><modules runAllManagedModulesForAllRequests="true"><remove name="ScriptModule" /><remove name="UrlRoutingModule" /><add name="ScriptModule" preCondition="managedHandler"type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><add name="UrlRoutingModule"type="System.Web.Routing.UrlRoutingModule,System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /></modules><handlers><remove name="WebServiceHandlerFactory-Integrated"/><remove name="ScriptHandlerFactory" /><remove name="ScriptHandlerFactoryAppServices" /><remove name="ScriptResource" /><remove name="MvcHttpHandler" /><remove name="UrlRoutingHandler" /><add name="ScriptHandlerFactory" verb="*" path="*.asmx"preCondition="integratedMode"type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><add name="ScriptHandlerFactoryAppServices" verb="*"path="*_AppService.axd" preCondition="integratedMode"type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><add name="ScriptResource" preCondition="integratedMode"verb="GET,HEAD" path="ScriptResource.axd"type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /><add name="MvcHttpHandler" preCondition="integratedMode"verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><add name="UrlRoutingHandler"preCondition="integratedMode" verb="*" path="UrlRouting.axd"type="System.Web.HttpForbiddenHandler,System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /></handlers></system.webServer></configuration>
  4. Add the following to Global.asax.cs (create a Global.asax if you don’t already haveone)
    public static void RegisterRoutes(RouteCollectionroutes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");routes.MapRoute("Default", //Route name"{controller}/{action}/{id}", //URL with parametersnew { controller = "Home",action = "Index", id = "" } //Parameter defaults);}protected void Application_Start(){RegisterRoutes(RouteTable.Routes);}

    The above is the “standard” routing for a MVC site. It will work, but it might causeyou trouble if you want the root of your website to still go to a ‘default.aspx’.Try this line instead:

    routes.MapRoute("Default", "MVC/{controller}/{action}/{id}", new { controller = "Home",action = "Index", id = "" }
  5. Edit your .csproj file by hand
    <ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

    This will tell Visual Studio to act like this is an MVC project. You will getthe context menu items for MVC in your Controllers and Views directories.
    Note: I’ve had trouble at this point with some Visual Studio installs.If you’re having trouble with “The project type is not supported by this installation.”messages here, it may be time for a clean install in a fresh VM.
  6. Set Up IIS. If you’re using IIS7, the System.Webserver settings in your Web.Configfile should have taken care of this step. If you’re going to need to map the wildcardURL to aspnet_isapi.dll in order to get all of the MVC routing magic to work.

  7. You might want to copy of the /Views/web.config file from an existing MVC projectto prevent your views from being viewed directly, rather than through their controllers.
  8. You might also wish to create a /Scripts folder and copy over the contents ofthe /Scripts folder from native MVC project. It has the jquery and MS AJAX javascriptsthat you may be looking for if you’re following a MVC book or tutorial.

Boy howdy. That was a good chunk of work. But now you have the infinite pleasure ofdeveloping in MVC, and your old web site should continue to work under you. Deliciousincremental development goodness.

Resources

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s