How to upgrade Asp.Net MVC 4 project to MVC 5

How to upgrade Asp.Net MVC 4 project to MVC 5

How to upgrade Asp.Net MVC 4 project to MVC 5

There are many of benefits of upgrading your project to ASP.NET MVC framework. If you wish to upgrade this is the tutorial for you.

ASP.Net MVC 5 bring a host of new features, for example attribute routing, authentication filters etc. Go to this link for more details.

Upgrade to MVC 5 step-by-step guide :

  • First backup your project

    If something goes wrong, you’ll have a working backup.

  • In your global.asax file, change
    WebApiConfig.Register(GlobalConfiguration.Configuration)

    to this

    GlobalConfiguration.Configure(WebApiConfig.Register)
  • Check your packages

    Check to see that all your packages , that your project is using, are compatible with MVC 5.

  • Open your project

    This step is self-explanatory, open your project in Visual Studio

  • Remove NuGet Packages

    Remove any of the following ASP.NET NuGet packages that are installed. You can remove these using PMC ( Package Manager Console), in PMC run this following commands :

    Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration
    Uninstall-Package -Id Microsoft-Web-Helpers
    Uninstall-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes

    * Note that your project might not include all of these packages.

  • Upgrade NuGet Packages

    In the PMC run the following :

    Update-Package *

    This command will update every package, if you wish, you can update packages individually using ID argument.

  • Update Application web.config File

    Find  <runtime>/<assemblyBinding> , and make the following changes :

    for “System.Web.Mvc” change the version from number “4.0.0.0” to “5.0.0.0” ->

    for “System.Web.Helpers” and “System.Web.Webpages” change the version from “2.0.0.0” to “3.0.0.0”

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <!--Two elements removed for Clarity -->
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <!--WebGrease element removed for Clarity -->
      </assemblyBinding>
  • Update webpages:version

    Locate the <appSettings> section and update the webpages:version from 2.0.0.0.0 to 3.0.0.0.

      <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>
  • Remove trust levels

    Remove all trust levels other than Full

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

    Update the web.config files , in your views folder – update all elements that contain

    “System.Web.Mvc” from version “4.0.0.0.” to “5.0.0.0.”

    <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>
          <add namespace="System.Web.Mvc" />
          <!--Elements removed for Clarity.-->
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    <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>
      </pages>
    </system.web>
  • Update Razor elements

    Update all elements that contain “System.Web.WebPages.Razor” from “2.0.0.0.” to “3.0.0.0”

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

    * note that if there is “System.Web.WebPages” in this section, update those elements from “2.0.0.0” to “3.0.0.0”

  • Install Microsoft-Web-Helpers

    Go back to PMC to install Microsoft-Web-Helpers again, run the following :

    Install-Package –Id Microsoft.AspNet.WebHelpers
  • User.IsInRole() method

    If your app use User.IsInRole() method, add the following to the Web.config file :

  • Build and Test

    remove MVC 4 project type GUID from project files , to do that you’ll need to:

    -> open Solution Explorer, right click the project Unload Project.

    -> Right click it again, select Edit YourProject.csproj

    -> find ProjectTypeGuids element, and then remove MVC 4 GUID

    -> save , close project.

    > right click, and select Reload Project

Conclusion

It's not as hard as it looks, just follow simple steps from above and you'll be just fine. If you are searching guide on how to upgrade Asp.Net MVC 3 to MVC 5, you'll need to read this article first and then come back to this one.

Posted by Ivan Bašić

Ivan Bašić
I am Digital Marketing Consultant working @ Ingenium. If you wish you can, Send an Email to me

Related Posts

Comments

comments powered by Disqus