Razor 2 to Razor 3 MVC 5

42,489

Solution 1

In your Web.config(-s) make sure assemblyBinding contains the proper version for the assembly System.Web.WebPages.Razor and System.Web.Mvc.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" 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>
</assemblyBinding>

And make sure that razor sectionGroup in ConfigSections reference latest versions as well:

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

Solution 2

Check for version in web.config. If it again gives an error, try to clean the solution and rebuild it. Also check for project's Bin folder, removes old references from bin folder and rebuild the project solution.

Share:
42,489
Dylan Corriveau
Author by

Dylan Corriveau

Updated on July 09, 2022

Comments

  • Dylan Corriveau
    Dylan Corriveau almost 2 years

    I've been working on an MVC 4 solution, and I've been trying to upgrade it to MVC 5. I've followed the steps outlined here.

    I've followed it, and now whenever I run the MVC Application, it gives me this error message:

    [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to 
    [B]System.Web.WebPages.Razor.Configuration.HostSection. 
    
    Type A originates from 
    'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, 
    PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
    'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. 
    Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, 
    PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
    'C:\Users\User\AppData\Local\Temp\Temporary ASP.NET 
    Files\root\665ac028\de53a189\assembly\dl3\c2c0a4b5\56e8099e_40e0ce01\System.Web.WebPages.Razor.dll'.
    

    Does anyone know how this could have originated? or how it can be solved? I've looked around thus far? I've tried changing the web.config files, with no avail...

  • Dylan Corriveau
    Dylan Corriveau over 10 years
    in the binding redirect, MVC has 0-5, new version 5, and razor has 1-3 new version 3
  • Dylan Corriveau
    Dylan Corriveau over 10 years
    You were correct. Although I did restart the application (I wasn't very far in it, so I just made an MVC 5 project and ported it), I noticed the issue was that in the second web config after the fact (the one inside of views)
  • Dima
    Dima over 10 years
    Exactly, because you may have configs in literally any part of the project, it's essential to look through/correct all of them
  • Dylan Corriveau
    Dylan Corriveau over 10 years
    thanks Dima :). How many web.config's are there usually, just the two, right? or does that depend?
  • Dima
    Dima over 10 years
    Default MVC website template has 2 configs: one in website root directory and another one in Views. Mvc team plays with templates changing them from version to version, so it's better to use file search to be sure you haven't missed any.
  • Rodolpho Brock
    Rodolpho Brock about 10 years
  • ManirajSS
    ManirajSS almost 10 years
    what we have to do dependentAssembly not at all have System.Web.WebPages.Razor ???
  • Howiecamp
    Howiecamp almost 10 years
    @Dima - Would the public key token have to change since the version of the library changed?
  • Dima
    Dima almost 10 years
    @Howiecamp they all signed with the same private Microsoft's key, which haven't been changes for a long time (if ever). So no, public key token wouldn't change with new library version.
  • Arvind Dhasmana
    Arvind Dhasmana over 8 years
    @Dima I changed my project from Framework 4.0 to 4.5.1 and install the MVC 5.2.3 and faced this same issue. I follow your answer and found that only following was missing in main web.config, <dependentAssembly> <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/> </dependentAssembly>
  • Gonzalo Méndez
    Gonzalo Méndez about 8 years
    Worked perfectly to me. Thanks dude!