115

I have a problem like this on server

[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=1.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_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: [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=1.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_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.

Source Error:

I have installed MVC 3 on serer and on my computer. I have tried with replacing it with 15 days old codebase but the same problem exist.

now I want to solve it. Do someone know how I can fix it.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
user2632851
  • 1,169
  • 2
  • 7
  • 5
  • Seems like in your application some assemblies have a dependencies on different versions of razor. Try inspecting your assemblies with ildasm or .NET Reflector to see its dependencies. – Paulo Morgado Jul 30 '13 at 23:23

9 Answers9

200

In the root Web.config make sure assemblyBinding contains the proper version for the assembly "System.Web.WebPages.Razor" and "System.Web.Mvc". Check for their actual existence as well as my "System.Web.WebPages.Razor" assembly tag was missing causing the error. I had my assembly versions set to 3.0 and 5.0 respectively in the time of this writing with full NuGet updates. Code should look something like below. The publicKeyToken will stay the same between the versions.

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <!--some more bidings-->
  <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>
<!--some more bidings-->
</assemblyBinding>
starball
  • 20,030
  • 7
  • 43
  • 238
Ian Flynn
  • 3,577
  • 2
  • 18
  • 14
  • 2
    Worked for me. I was missing Razor information and MVC pointed to the wrong version. – Hoakie Oct 23 '13 at 13:39
  • Same situation as @Hoakie - updated Razor to 3.0.0 and MVC to 5.0.0 and there was no Razor dependentAssembly reference, and MVC still said oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0". Added the Razor dependentAssembly XML above and changed 4.0.0.0 to 5.0.0.0 and Bob's yer uncle. – Scott Baker Oct 29 '13 at 17:27
  • 2
    This worked for me thanks. I did have "System.Web.WebPages" with the correct bindings but it wasn't working until I also added the Razor key. However in other projects I didn't have to do that, I'm not sure what the difference is and why the super-class bindings wouldn't handle for Razor. Does anyone know the answer? – Shanerk Oct 31 '13 at 15:02
  • Same here. I have the MVC entry but not the Razor one. It now works. Thank you. – Yves Tkaczyk Nov 07 '13 at 21:26
  • This works https://stackoverflow.com/questions/17454852/upgrading-from-mvc4-to-mvc5-pre-release – Rodolpho Brock Mar 26 '14 at 20:28
  • When I saw this error, I expected to have to waste hours following dependency chains looking for broken reference. You've just spared me all that. Thanks! – Basic Jul 18 '14 at 22:31
  • After 3 hours of searching, I found this solution. thanks bro, working great. – Ibraheem Al-Saady Aug 31 '14 at 13:57
  • Worked for me too, thanks very much. Problems happened after upgrading MVC4 to 5. – Lee Oct 04 '16 at 14:52
40

My case was upgrading from mvc4 to mvc5.

I made sure my references were in sync (with a newly created mvc5 app), updated the web.config file accordingly and it still didn't work. It finally worked when i changed my web.config file (INSIDE THE VIEWS directory) from:

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

To this:

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

REMEMBER: stop iis (express or native), clean solution, build and Run.

user3141889
  • 691
  • 7
  • 3
  • i think this response should be an answer actually. Assembly binding is usually ok when using update-package approach to upgrading from mvc4 to mvc5. – Roman Jan 20 '15 at 06:14
34

Just in case ian's answer wasn't enough (and the assemblies correct, but the casting is still wrong) chances are you didn't update the <configSections> to reflect the new assembly yet. make sure the assembly portion also references version 3.0 of the System.Web.WebPages.Razor library. e.g.

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

(Notice that they all reference version=3.0.0 now)

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
15

Ian's solution worked in one project, but for others I need the more complete info in How to Upgrade to MVC 5 and Web API 2

Ken Mc
  • 1,268
  • 15
  • 12
2

Problem:

[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:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\rbs.ceds.todomvcdemo\73d0b7c7\46e3986d\assembly\dl3\21020800\101279fc_da94cf01\System.Web.WebPages.Razor.dll'.

Identification:

The error is with the configuration file i.e. web.config of your project.

Here you will find that Razor version is set to 2.0.0.0 where as in your project 2.0.0.0 version is not available.

Solution:

It is simple, just change the version from 2.0.0.0 to 3.0.0.0 and it will do the magic.

type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0 type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0

You don't need to put redirection or so as it will make things complicated.

codebased
  • 6,945
  • 9
  • 50
  • 84
1

This issue is very common when you are developing for MVC 4 and then suddently you install a package which is available in newer version so it breaks entire application.

Only solution to such issue is to upgrade your entire application to newer or install the old package compatible with your application

Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
0

I found a problem and solution.

1 I target my code to 4.0 and then upload the code and it's worked. Before it I also update my code to asp.net MVC 4.

now it's work :)

user2632851
  • 1,169
  • 2
  • 7
  • 5
0

Open the two dll files location in your system copy one of the dll in your file location to the another
and try to run the project.

0

Another way to fix this, without having to update references is this

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Pages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="1.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>