"
ASP.NET (snapshot 2017) Microsoft documentation and samples

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

by Rick Anderson

ASP.NET MVC 5 and Web API 2 bring a host of new features, including attribute routing, authentication filters, and much more. See https://www.asp.net/vnext for more details.

This walkthrough will guide you with the steps required to upgrade your application to the latest version.

[!NOTE] Please see ASP.NET and Web Tools for Visual Studio 2013 Release Notes for information on breaking changes from MVC 4 and Web API to the next version.

This article was written by Youngjune Hong and Rick Anderson ( [@RickAndMSFT](https://twitter.com/#!/RickAndMSFT) )

Upgrade Steps

  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:

    [!code-csharpMain]

       1:  WebApiConfig.Register(GlobalConfiguration.Configuration);

to

[!code-csharpMain]
   1:  GlobalConfiguration.Configure(WebApiConfig.Register);
  1. 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.

    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.WebData 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.SelfHost 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi.Client 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi.OData 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi.WebHost 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi.Tracing 4.0.x.x 5.0.0
    Microsoft.AspNet.WebApi.HelpPage 4.0.x.x 5.0.0
    Microsoft.Net.Http 2.0.x. 2.2.x.
    Microsoft.Data.OData 5.2.x 5.6.x
    System.Spatial 5.2.x 5.6.x
    Microsoft.Data.Edm 5.2.x 5.6.x
    Microsoft.AspNet.Mvc.FixedDisplayModes Removed
    Microsoft.AspNet.WebPages.Administration Removed
    Microsoft-Web-Helpers Microsoft.AspNet.WebHelpers

    [!NOTE] Microsoft-Web-Helpers has been replaced with Microsoft.AspNet.WebHelpers. You should remove the old package first, and then install the newer package.

    There is no cross version compatibility among major ASP.NET packages. For example, MVC 5 is compatible with only Razor 3, and not Razor 2.

  2. Open your project in Visual Studio 2013.
  3. 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.

    1. Microsoft.AspNet.WebPages.Administration
      This package is typically added when upgrading from MVC 3 to MVC 4. To remove it, run the following command in the PMC:
      Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration
    2. Microsoft-Web-Helpers
      This package has been rebranded as Microsoft.AspNet.WebHelpers. To remove it, run the following command in the PMC:
      Uninstall-Package -Id Microsoft-Web-Helpers
    3. 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
  4. Upgrade all the ASP.NET NuGet packages using the PMC. In the PMC, run the following command:
    Update-Package
    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 <runtime>/<assemblyBinding> 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.

    [!code-xmlMain]
       1:  <runtime>
       2:    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       3:      <!--Two elements removed for Clarity -->
       4:      <dependentAssembly>
       5:        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
       6:        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
       7:      </dependentAssembly>
       8:      <dependentAssembly>
       9:        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      10:        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      11:      </dependentAssembly>
      12:      <dependentAssembly>
      13:        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
      14:        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      15:      </dependentAssembly>
      16:      <!--WebGrease element removed for Clarity -->
      17:    </assemblyBinding
  3. Locate the <appSettings> section and update the webpages:version from 2.0.0.0.0 to 3.0.0.0 as shown below:

    [!code-xmlMain]
       1:  <appSettings>
       2:      <add key="webpages:Version" value="3.0.0.0" />
       3:      <add key="webpages:Enabled" value="false" />
       4:      <add key="PreserveLoginUrl" value="true" />
       5:      <add key="ClientValidationEnabled" value="true" />
       6:      <add key="UnobtrusiveJavaScriptEnabled" value="true" />
       7:    </appSettings>
  4. Remove any trust levels other than Full. For example:

    [!code-xmlMain]

       1:  <securityPolicy>
       2:     <!--<trustLevel name="Medium"  policyFile="web_mediumtrust.config"/>-->
       3:  </securityPolicy>

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”.

    [!code-xmlMain]

       1:  <system.web.webPages.razor>
       2:    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       3:    <pages pageBaseType="System.Web.Mvc.WebViewPage">
       4:      <namespaces>
       5:        <add namespace="System.Web.Mvc" />
       6:        <!--Elements removed for Clarity.-->
       7:      </namespaces>
       8:    </pages>
       9:  </system.web.webPages.razor>

    [!code-xmlMain]
       1:  -->
       2:    <pages
       3:        validateRequest="false"
       4:        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
       5:        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
       6:        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
       7:      <controls>
       8:        <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
       9:      </controls>
      10:    </pages>
      11:  </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”

    [!code-xmlMain]
       1:  <configuration>
       2:    <configSections>
       3:      <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">
       4:        <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" />
       5:        <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" />
       6:      </sectionGroup>
       7:    </configSections>
  3. If you removed the Microsoft-Web-Helpers NuGet package in a previous step, install Microsoft.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.

    [!code-xmlMain]

       1:  <system.webServer>
       2:      <modules>
       3:        <remove name="RoleManager" />
       4:      </modules>
       5:    </system.webServer>

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.


Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/releases/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2.htm
< THANKS ME>