Search This Blog

Thursday, March 13, 2014

How to Upgrade an ASP.NET MVC 4 and Web API Project to ASP.NET MVC 5 and Web API 2

To upgrade an ASP.NET MVC 4 and Web API Project to ASP.NET MVC 5 and Web API 2


follow this simple and easy steps to upgrade MVC 4 to MVC 5 Project :

1.Backup your project. This walkthrough will require you to make changes  to your project file, package configuration, and web.config files.

2.For upgrading from Web API to Web API 2, in global.asax, change:     

          WebApiConfig.Register(GlobalConfiguration.Configuration)
           to       
         GlobalConfiguration.Configure(WebApiConfig.Register);

3.Make sure all the packages that your projects use are compatible with  MVC 5 and Web API 2. The following table shows the MVC 4 and Web API related  packages than need to be changed. If you have a package that is dependent on  one of the packages listed below, please contact the publishers to get the  newer versions that are compatible with MVC 5 and Web API 2. If you have the  source code for those packages, you should recompile them with the new  assemblies of MVC 5 and Web API 2. 

4.Open your project in Visual Studio 2013.

5.Remove any of the following ASP.NET NuGet packages that are installed.  You will remove these using the Package Manager Console (PMC). To open the  PMC, select the Tools menu and then select Library  Package Manager, then select Package Manager Console.  Your project might not include all of these.Microsoft.AspNet.WebPages.Administration 
  1. This package is typically added when upgrading from MVC 3 to MVC 4.  To remove it, run the following command in the PMC:
  2. Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration
Microsoft-Web-Helpers 
  1. This  package has been rebranded as Microsoft.AspNet.WebHelpers.  To remove it, run the following command in the PMC:
  2. Uninstall-Package -Id Microsoft-Web-Helpers
Microsoft.AspNet.Mvc.FixedDisplayMode  
This package contains a work around for a bug in MVC 4 that has been  fixed in MVC 5. To remove it, run the following command in the PMC:
Uninstall-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes

The Update-Package command without any parameters will update every  package. You can update packages individually by using the ID argument. For  more information about the update command, run get-help update-package.

Update the Application web.config File

Be sure to make these changes in the app web.config file, not the web.config file in the Views folder.
Locate the / section, and  make the following changes:
  1. In the elements with the name attribute “System.Web.Mvc”, change the  version number from “4.0.0.0” to “5.0.0.0”. (Two changes in that element.)
  2. In elements with the name attribute "System.Web.Helpers” and  "System.Web.WebPages" change the version number from “2.0.0.0” to “3.0.0.0”.  Four changes will occur, two in each of the elements.

   xmlns="urn:schemas-microsoft-com:asm.v1">
    
    
       name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
       oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
    
          name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />       oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />               name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />       oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />          
3.Locate the  section and update the  webpages:version from 2.0.0.0.0 to 3.0.0.0 as shown below:

     key="webpages:Version" value="3.0.0.0" />
     key="webpages:Enabled" value="false" />
     key="PreserveLoginUrl" value="true" />
     key="ClientValidationEnabled" value="true" />
     key="UnobtrusiveJavaScriptEnabled" value="true" />
  
4.Remove any trust levels other than Full. For example:

   

Update the web.config files under the Views folder

If your application is using areas, you will also need to update each web.config file in the Views sub-folder of each Area folder.
  1. Update all elements that contain “System.Web.Mvc” from version “4.0.0.0”  to  version“5.0.0.0”.

   factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
   pageBaseType="System.Web.Mvc.WebViewPage">
    
       namespace="System.Web.Mvc" />
      
    
 

-->
  <pages
      validateRequest="false"
      pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
      pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
      userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    
      <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
    </controls>
  </pages>
</system.web>
  2. Update all elements that contain “System.Web.WebPages.Razor”  from  version “2.0.0.0”  to  version“3.0.0.0”. If this section contains “System.Web.WebPages”,  update those elements from  version “2.0.0.0” to  version“3.0.0.0”


  
     name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      
name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />      
name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />    
 
 3.If you removed the Microsoft-Web-Helpers NuGet package in a  previous step, installMicrosoft.AspNet.WebHelpers  with  the following command in the PMC:

Install-Package -Id  Microsoft.AspNet.WebHelpers
 4. If your app uses the  User.IsInRole() method, add the following to the Web.config file.

    
       name="RoleManager" />
    

Final Steps

Build and test the application.
 Remove the MVC 4 project type GUID from the project files.
  1. In Solution Explorer, right-click the project name and then select Unload Project.
  2. Right-click the project  and select Edit ProjectName.csproj.
  3.  Locate the ProjectTypeGuids element and then remove  the MVC 4 project GUID, {E3E379DF-F4C6-4180-9B81-6769533ABE47}.
  4. Save and close the open project file.
  5. Right-click the project and select Reload Project.













































No comments:

Post a Comment