51

What steps should I take to safely upgrade an existing ASP.NET MVC 3 project to the latest ASP.NET MVC 4 Developer Preview?

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287

4 Answers4

43

The major change is to upgrade the relevant references to the latest version (4.0 /2.0). You need to update your web config files to upgrade the version of the following namespaces

  • System.Web.Mvc ( Change to 4.0.0.0)
  • System.Web.Webpages (Change to 2.0.0.0)
  • System.Web.Helpers (Change to 2.0.0.0)
  • System.Web.WebPages.Razor (Change to 2.0.0.0)

Also you need to update the root level web config file to have these appsettings entries

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

Then you need to update the DLLs referenced in the project. Remove the existing MVC3 dll reference and add MVC4 (use nuget package manager to do this)

This link handles all aspects of the conversion. Refer it as needed.

MMalke
  • 1,857
  • 1
  • 24
  • 35
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 1
    I needed to do an additional step: I deleted references in my project to System.Web.Mvc, System.Web.WebPages, System.Web.Helpers, and added references for the newer versions. – Rn222 Aug 21 '12 at 19:47
18

Follow the official steps posted here:

Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4

Note: These steps cover the official release--not the developer preview.

MMalke
  • 1,857
  • 1
  • 24
  • 35
Seth
  • 6,514
  • 5
  • 49
  • 58
  • 1
    This link was already provided in [Shyju's answer](http://stackoverflow.com/a/8853408/2188245), along with a description. – Th4t Guy Aug 13 '14 at 21:43
  • 1
    Yes it was, but he left out some steps, and this isn't a procedure that should be reproduced on StackOverflow.com. Developers should go straight to the source without passing Go. – Seth Aug 13 '14 at 22:20
7

Better yet, use the auto-update tool:

https://www.nuget.org/packages/UpgradeMvc3ToMvc4

Justin
  • 17,670
  • 38
  • 132
  • 201
  • 1
    I found this to be the quick, simple and easy method, but it also added in the OAuth packages which I didn't need. – Dan Atkinson Jan 30 '14 at 14:00
  • This failed for me with no real reason (didn't write down the error, sorry). Had to go the manual route. – willem Jun 05 '14 at 09:16
  • That's a far too invasive tool... makes far too many presumptions including jamming in a whole host of openAuth stuff. For intranet windows auth apps it simply breaks everything. Use with caution. – Dave Lawrence Feb 17 '15 at 09:38
  • While yes it jams stuff in there, it really worked for me. I can easily remove what I don't need; thanks for saving me time converting a very old project! ;) – James Wilkins Dec 01 '18 at 09:51
  • Vau great! Worked first time! Thx. – sabiland Jun 17 '20 at 09:46
4

Your best bet it's installing the MVC4 from the platform installer. Then follow the steps on the official asp.net/mvc/mvc4 page. If you follow (only) the Shyju instructions It wouldn't work.

When you end with the official instructions, look on your Views directory, and you may find another web.config with another reference to the MVC assembly and several others for razor. You also want to change the versions of this lines. Basically replace every 3.0.0.0 you find for 4.0.0.0 and 1.0.0.0 for 2.0.0.0

IF YOU DON'T MAKE THIS LAST CHANGE, YOU MAY END WITH THE ERROR ON THE FOLLOWING QUESTION

Look for included dll's and make sure they're for the correct versions.

Community
  • 1
  • 1
Sergio
  • 1,383
  • 2
  • 13
  • 28