March 27, 2012
MVC 4.0 living side by side with ASP .NET legacy website project (not web application)

Just came across an interesting problem:

I wanted to use MVC 4.0 within a legacy asp .net website project ( not a web application) using Visual Studio 2010 and Razor. I have all MVC versions installed on my machine.

So first I added/registered all MVC assemblies to web.config:

 <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <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" />
     
     

Also added to the <pages> node:

 <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>
      <!——>
      <controls>
        <add tagPrefix=”asp” namespace=”System.Web.UI” assembly=”System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35” />
        <add tagPrefix=”asp” namespace=”System.Web.UI.WebControls” assembly=”System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35” />
      </controls>

And add the correct <httpmodules> :

 <httpModules>
      <!—<add name=”HttpCompressModule” type=”Ngd.DreamTeam.Football.Web.Compression.HttpModule,Ngd.DreamTeam.Football.Web”/>—>
      <add name=”ScriptModule” 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, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”/>
    </httpModules>

 <modules runAllManagedModulesForAllRequests=”true”>
      <add name=”UrlRoutingModule” type=”System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”/>
      <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” />
    </modules>

Under <handlers> :

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

Then merge the global asax and put the methods all on the global.asax.cs even if they are on the Global.asax .

So Global.asax should look like this :

<%@ Application Language=”C#” CodeFile=”Global.asax.cs” Inherits=”MvcApplication” %>

and the Global.asax.cs like this :


    public partial class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        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 }
            );


        }

        protected void Application_Start()
        {



            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            // This is the default setting for displaying marketing modals
            SessionManager.SessionManagerInstance.ShowMarketingMessage = true;
        }

        protected void Application_Error(object sender, EventArgs e)
        {
         // all the legacy stuff

             }

        protected void Session_End(object sender, EventArgs e)
        {
            Session.Clear();
            Session.Abandon();
        }

        protected void Application_End(object sender, EventArgs e)
        {

                   }

    }

Then created a folder Views on the root of the project and put the controllers classes into a folder called Controllers inside the App_Code folder ( If the controllers class are outside the App_Code project MVC will not work).

After all this the project did not work so after a couple of hours I found out that the virtual path ( on VS right button properties on top of the website node) cannot contain the character ‘.’ . My virtual path was something like “project.web” . When I removed this I successfully added MVC 4.0 to a legacy website asp .net project.