Upgrading to MVC 4 (from prior versions)




- April 20, 2015

Rest of the Story:

MVC 4 will happily run alongside ASP.NET MVC version 3 and .NET 4/VS2010. You can thus experiment and create MVC 4 projects without fear of breaking your old applications.

There are three main methods of upgrading to MVC 4:

  • Copy existing content into a new MVC 4 project
  • Manually upgrade the project
  • Utilize the MVC 4 NuGet package (by applying the Microsoft.AspNet.MVC package)

 Manual Upgrade Steps:

Open up all the web.config files in your project and replace any lines that read as the following:

System.Web.Mvc, Version=3.0.0.0  
System.Web.WebPages, Version=1.0.0.0  
System.Web.Helpers, Version=1.0.0.0  
System.Web.WebPages.Razor, Version=1.0.0.0

With their MVC 4 counterparts:

System.Web.Mvc, Version=4.0.0.0 System.Web.WebPages, Version=2.0.0.0 System.Web.Helpers, Version=2.0.0.0, System.Web.WebPages.Razor, Version=2.0.0.0,
In the root web.config file, add a new PreserveLoginUrl key entry:

<appSettings>  
  <add key="webpages:Version" value="2.0.0.0" />  
  <add key="PreserveLoginUrl" value="true" />  
<appSettings>

Now delete any references to System.Web.MVC (v3). In Solution Explorer, remove the following assembly references:

  • System.Web.Mvc (v3.0.0.0)
  • System.Web.WebPages (v1.0.0.0)
  • System.Web.Razor (v1.0.0.0)
  • System.Web.WebPages.Deployment (v1.0.0.0)
  • System.Web.WebPages.Razor (v1.0.0.0)
  • Now add references to the new versions of these assemblies:
  • System.Web.Mvc (v4.0.0.0)
  • System.Web.WebPages (v2.0.0.0)
  • System.Web.Razor (v2.0.0.0)
  • System.Web.WebPages.Deployment (v2.0.0.0)
  • System.Web.WebPages.Razor (v2.0.0.0)

In Solution Explorer, unload your MVC project as we are going to make some changes to the project file; this won't work if the solution is open.
Open the project file  and replace any references of the ProjectTypeGuids E53F8FEA-EAE0-44A6-8774-FFD645390401 with E3E379DF-F4C6-4180-9B81-6769533ABE47.
Save the changes you have made and reload the project.
Finally, if your application or its references uses any assemblies compiled against the previous version of MVC, tell these to use MVC4 by adding binding redirect entries such as the following:

<configuration>  
  <runtime>  
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
      <dependentAssembly>  
        <assemblyIdentity name="System.Web.Helpers"  
             publicKeyToken="31bf3856ad364e35" />  
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>  
      </dependentAssembly>  
      <dependentAssembly>  
        <assemblyIdentity name="System.Web.Mvc"  
             publicKeyToken="31bf3856ad364e35" />  
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0"/>  
      </dependentAssembly>  
      <dependentAssembly>  
        <assemblyIdentity name="System.Web.WebPages"  
             publicKeyToken="31bf3856ad364e35" />  
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>  
      </dependentAssembly>  
    </assemblyBinding>  
  </runtime>  
</configuration>
 

Notes:

  • I had to do similar updates to the web.config located in the Views directory

  • If after you build and browse to the home page, you get errors like ‘The type or namespace {fill in} does not exist in the namespace..you are missing an assembly reference’ you may need to set ‘Copy Local = True’ on a few of the references.  In particular I had to set to True for the following assemblies:

  • Microsoft.Web.Infrastructure

  • System.Web.Helpers

  • System.Web.Http

  • System.Web.Http.WebHost

  • System.Web.Mvc

  • System.Web.Razor

  • System.Web.WebPages

  • System.Web.WebPages.Deployment

  • System.Web.WebPages.Razor

  • WebGrease

ASP.NET MVC 4 new features - http://www.asp.net/whitepapers/mvc4-release-notes

Note: Also note .NET 4.5 is an extension to 4.0, it does not introduce a new runtime