Promoting MVC 4 and Web API Project to MVC 5 and Web API 2

Introduction

This article will introduce you to enhancing MVC 4 and Web API projects based Web Application to MVC 5 and Web API 2. As you already know, MVC 5 and Web API 2 have many new features like attribute routing, authentication filters and so on. I am giving you a brief description to upgrade the application to the most recent version.

So, let's proceed with the following sections:

  • Working with MVC 4 Project
  • Working with Visual Studio 2013
  • Removing the MVC 4 Project GUID

Working with MVC 4 Project

Step 1: Please create a backup of your project. There are many changes made in the application with this walkthrough.

Step 2: If you want to upgrade the Web API to Web API 2, then open the Global.asax file and replace the following code:

WebApiConfig.Register(GlobalConfiguration.Configuration); 

with the code below:

GlobalConfiguration.Configure(WebApiConfig.Register); 

Step 3: You need to be sure that the packages that you are using are suitable with MVC 5 and Web API 2. I am presenting the following table that shows that the MVC 4 and Web API related  packages than need to be changed. A few of the packages with their old version and new version are below:

Package Id Old Version New Version
Microsoft.AspNet.Razor 2.0.x.x 3.0.0
Microsoft.AspNet.WebPages 2.0.x.x 3.0.0
Microsoft.AspNet.WebPages.OAuth 2.0.x.x 3.0.0
Microsoft.AspNet.Mvc 4.0.x.x 5.0.0
Microsoft.AspNet.Mvc.Facebook 4.0.x.x 5.0.0
Microsoft.AspNet.WebApi.Core 4.0.x.x 5.0.0
Microsoft.AspNet.WebApi 4.0.x.x 5.0.0
Microsoft.AspNet.Mvc.FixedDisplayModes   Removed
Microsoft.AspNet.WebPages.Administration   Removed

Note: MVC 5 is only compatible with Razor 3.

Working with Visual Studio 2013

Now we'll work with the Visual Studio 2013 (RTM Version). Please use the following procedure.

Step 1: Click on "Open Project" and select the existing MVC 4 application.

Open Mvc4 Project in VS 2013

Step 2: Open the Package Manager Console.

Open Package Manager Console

Step 3: You remove the following NuGet Packages from the application. Your project might not include all of these.

  1. Microsoft.AspNet.WebPages.Administration

    This package is added, when upgrading from MVC 3 to MVC 4. To remove it enter the following command:

    Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration
     
  2. Microsoft-Web-Helpers

    This package is replaced with Microsoft.AspNet.WebHelpers. To remove it enter the following command:

    Uninstall-Package -Id Microsoft-Web-Helpers
     
  3. Microsoft.AspNet.Mvc.FixedDisplayModes

    This package is used for working around a bug in MVC 4 that is fixed in MVC 5. To remove it enter the following command:

    Uninstal-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes

Step 4: Now you update all current packages in your application. Enter the following command in the Package Manager Console (PMC):

Update-Package

Updating Package in Package Manager Console

Note: You can update the package by giving the specific package name using the ID argument.

Updating the application Web.Config File

Now we'll make some changes in the Web.Config file of the application. So, proceed with the following procedure.

Step 1: Open the Web.Config file present in the root folder.

WebConfig File in Root Folder

Step 2: Please change the old version to the new version of elements. So please update the code as shown below with the highlighted code:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
       <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly> 

Step 3: Update the AppSettings as shown below with the highlighted code:

<appSettings>

  <add key="webpages:Version" value="3.0.0.0" />

  <add key="webpages:Enabled" value="false" />

  <add key="PreserveLoginUrl" value="true" />

  <add key="ClientValidationEnabled" value="true" />

  <add key="UnobtrusiveJavaScriptEnabled" value="true" />

</appSettings> 

Step 4: Remove any trust levels other than "Full". As an example:

 <securityPolicy>
 
   <!--<trustLevel name="Medium"  policyFile="web_mediumtrust.config"/>-->
 
</securityPolicy> 

Updating the Web.Config File in Views

Now we'll make some changes in the Web.Config file that exists in the Views Folder. So, proceed with the following procedure.

Step 1: Open the Web.Config file present in the Views folder

WebConfig File in Views Folder

Step 2: Please change the old version to the new version of elements that contains System.Web.Mvc. So please update the code as shown below with the highlighted code:

<system.web.webPages.razor>

    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

    <pages pageBaseType="System.Web.Mvc.WebViewPage">

      <namespaces> 

and

<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">
      <controls>
        <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc"tagPrefix="mvc" />
      </controls> 

Step 3: Please change the old version to the new version of elements that contains System.Web.WebPages.Razor. So please update the code as shown below with the highlighted code:

<configuration>
  <configSections>
    <sectionGroup 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">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections> 

Step 4: Install the following NuGet Package, if you removed it:

Install-Package -Id Microsoft.AspNet.WebHelpers

Installing WebHelpers by Package Manger Console

Removing the MVC 4 Project GUID

Please build your solution.

Now to remove the MVC 4 Project GUID, use the following procedure.

Step 1: Just right-click on your project and select "Unload Project by Solution Explorer".

Unloading Project

Step 2: Now, right-click to select "Edit ProjectName.csproj"

Edit the Unloaded Project

Step 3: Find the ProjectTypeGuids element and remove the MVC 4 project GUID  as shown below with the highlighted text:

<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> 

Remove the highlighted text in the element.

Step 4: Please save it and close the opened file.

Step 5: Finaly right-click on the project to select "Reload Project".

Reloading Project

Summary

This article introduced you to upgrade the MVC 4 and Web API project based Web application to the latest version, in other words MVC 5 and Web API 2. You can also learn to upgrade the existing application to the latest version.Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all