Dusan Czupek's blog

Blog related .NET, Android & web development

Recent posts

Tags

Categories

Navigation

Pages

    Archive

    Blogroll

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      MVC Part 2 - Adding MVC to existing project

      Sometimes you want to add MVC functionality into existing project. In order to achieve it you need to do some changes in project file, web config, IIS. I would like to describe minimal changes you have to do for it. After these changes Visual Studio will knows the MVC stuff like models, views and so, IIS would be able to run your app. Difference between regular MVC project and modified project like this is minimal, you will miss javascript files (jquery and MicrosoftAjax). If your VS support MVC projects you can find them zipped at

      Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache\CSharp\Web\1033\EmptyMvcWebApplicationProjectTemplatev2.0.cs.zip

      within a zip file there's folder Scripts where the js files are placed

      1) In project file do following:
      Extend existing ProjectTypeGuids like this:
      <ProjectTypeGuids>{F85E285D-A4E0-4152-9332-AB1D724D3325};... </ProjectTypeGuids>

      2) add references to
      System.Web.Mvc
      System.Web.Routing

      3) Create folders in the root of the app:
      Controllers
      Models
      Views
      Views\Shared

      4) update Global.asax code behind to following. If you’re already has some changes in global.asax.cs merge your changes with these

      using System.Web.Mvc;
      using System.Web.Routing;
      
      namespace MvcApplication1
      {
          public class MvcApplication : System.Web.HttpApplication
          {
              public static void RegisterRoutes(RouteCollection routes)
              {
                  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      
                  routes.MapRoute(
                      "Default", // Route name
                      "{controller}/{action}/{id}", // URL with parameters
                      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                  );
      
              }
      
              protected void Application_Start()
              {
                  AreaRegistration.RegisterAllAreas();
      
                  RegisterRoutes(RouteTable.Routes);
              }
          }
      }


      5) web.config changes. Be aware to do not overwrite your existing stuff in web.config
      into <system.web><assemblies> add

              <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
              <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      
      

      into <system.web><pages> add or extend namespaces

            <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>
      

      IIS6 changes:
      into <system.web><httpModules> add

      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      

      into <system.webServer><handlers> add

            <remove name="UrlRoutingHandler" />
            <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      
      

      IIS7 + changes:
      in <system.webServer> change <modules> to

          <modules runAllManagedModulesForAllRequests="true">
            <remove name="UrlRoutingModule" />
            <remove name="ScriptModule" />
            <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>
      
      

      Note: you can have both config sections for IIS6 and IIS7 in place in order to keep app compatible between IIS.

      6) Under IIS6 you need to also add mapping. Under "Home Directory"


      click on "Configuration" and on tab "Mappings" click on "Insert" in the "Wildcard application maps" section

      points executable to:
      %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll


      7) Under IIS7 or greater
      For Classic mode:
      under app or whole server click on "HandlerMappings",


      click on "Add Wildcard Script Map"


      points executable to
      %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

      name it whatever you want and confirm by clicking on YES


      Integrated Mode:
      Would be enough changes from #5 IIS7 section

      Posted: Nov 30 2010, 18:03 by dczupek | Comments (0) RSS comment feed |
      • Currently 0/5 Stars.
      • 1
      • 2
      • 3
      • 4
      • 5
      Filed under: MVC | dotNet

      Pingbacks and trackbacks (2)+

      Comments are closed