[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to... web.config issue

C#Web ConfigNugetNuget PackageNuget Package-Restore

C# Problem Overview


I am getting the following error:

> [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\MyName\AppData\Local\Temp\Temporary > ASP.NET > Files\root\63e7ff36\a1cb775d\assembly\dl3\8f568c18\9b7ddacf_d04dcf01\System.Web.WebPages.Razor.dll'.

I have searched across stackoverflow for similar issues and I used their solutions but it doesn't seem to fix the issue I am experiencing.

It suddenly occurred, I just pressed clean solution and this error popped up. I also started using nuget package restore on the solution. I updated my packages as well (using Update-package) so one of those might have something to do with this issue.

C# Solutions


Solution 1 - C#

I am using VS2013, MVC 5.2.2.0, Web Api 2. I have just changed the all versions from 2.0.0.0 to 3.0.0.0 of the following section of Web.config resides inside the View folder of my project.

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

And the problem gets solved. BINGO !!

Solution 2 - C#

Just adding my two cents to this issue.

I found that the cause of this error for me was that the Views folders web.config was referencing System.Web.WebPages.Razor, Version=2.0.0.0 when everything else was using 3.0.0.0

Seems the nuget-package upgrade didn't account for this folder somehow.

Solution 3 - C#

The cause of this error is the web.config in the Views folder referencing System.Web.WebPages.Razor, Version=2.0.0.0 instead of 3.0.0.0.

Typically this can happen after a nuget-package upgrade, which does not account for this folder.

Update the Views\Web.config file:

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

for more information on upgrading to MVC 5 http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

Solution 4 - C#

Tried all the methods above, and the issue was still not solved until I inserted the following lines in web.config directly under project folder.

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

Solution 5 - C#

If you get this with the Route Debugger then you need to update the web.config in the Views folder under the Area for the Route Debugger.

Solution 6 - C#

In my case, I've solved this by also updating the Web.config on the live server which the VS has updated upon upgrading to MVC 5

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaximView Question on Stackoverflow
Solution 1 - C#Kuntal GhoshView Answer on Stackoverflow
Solution 2 - C#Niclas LindqvistView Answer on Stackoverflow
Solution 3 - C#MaximView Answer on Stackoverflow
Solution 4 - C#Jian HuangView Answer on Stackoverflow
Solution 5 - C#Luke PuplettView Answer on Stackoverflow
Solution 6 - C#rajeemcariazoView Answer on Stackoverflow