161

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.

Trevor Reid
  • 2,533
  • 3
  • 23
  • 33
Maxim
  • 3,516
  • 6
  • 36
  • 58

6 Answers6

278

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

Kuntal Ghosh
  • 3,338
  • 2
  • 12
  • 18
  • 7
    This did the job for me. Thanks! – Joseph Woodward May 09 '15 at 02:50
  • 1
    This solved it for me. I'm converting a webforms app to MVC5. – Ray Oct 17 '15 at 04:13
  • 1
    @guneysus, how would you do the equivalent of Kuntal's answer without manual editing? – twm Nov 01 '15 at 16:50
  • @twm Generally `Update-Package` edits also `web.config` – guneysus Nov 01 '15 at 16:57
  • This is why I LOVE SO ! – Homunculus Reticulli Nov 16 '15 at 11:30
  • This almost solved my problem and pointed me in the right direction. The configSection in my views web.config was alreasy at version 3.0.0.0. But theres also an assemblyBinding section in the root web.config that needed to be upgraded from 2.0.0.0 to 3.0.0.0. Look for dependentAssembly System.Web.WebPages.Razor. Thanks – GELR Dec 11 '15 at 15:03
  • Also check main web.config for . Bumping that to 3.0.0.0 was the last fix for me. – Ian W Nov 24 '16 at 06:11
  • This worked for me, however I still can't call this using AJAX... any idea? It works if I debug the Web API from VS. If I create another project and call it through JQuery Ajax, I get an error. Any idea? UPDATE: I realized the `origins` in the header specifies which website will access it and once I fixed that, it worked fine. Thanks for the help. +1 – Si8 Jan 20 '17 at 17:17
  • This worked for me. Visual Studio 2019, Targeting .NET 4.6.1, MVC 5.2.7. Thanks! (before this, I had to change app key "webpages:Version" to 3.0.0.0, which is why I also had to change sectiongroup - noted above) – MikeDev Feb 05 '20 at 18:14
  • You are a star! – ppenchev Feb 25 '21 at 16:13
103

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.

Niclas Lindqvist
  • 1,347
  • 2
  • 15
  • 24
59

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

Alex C
  • 15,136
  • 17
  • 61
  • 95
Maxim
  • 3,516
  • 6
  • 36
  • 58
  • 5
    it doesn't sound like a fix rather work around to me. some projects might be depending on mvc4 only – stenly Aug 10 '14 at 22:40
  • 12
    In particular, it looks like the fix for this error isin the "Update the web.config files under the Views folder" section. It seems updating from MVC4 -> MVC5 with NuGet doesn't automatically update the version numbers in Views/Web.config. – Tyler Aug 14 '14 at 15:04
  • Thanks for the link! It would have been nice if the NuGet update could have at least shown a link to that page... – Jedidja Aug 29 '14 at 14:59
  • I followed the guide and now I can't run my site in debug mode - tells me "... Project with output type of Class Library cannot be started..." – Matt Sep 02 '14 at 19:35
  • Follow-up: if you go to this other question (http://stackoverflow.com/questions/19456648/how-can-i-get-my-web-api-app-to-run-again-after-upgrading-to-mvc-5-and-web-api-2) you will see the second answer helped me after this broke down. – Matt Sep 02 '14 at 19:44
  • 2
    Worked for me, my primary issue was caused because I hadn't updated the versions in the web.config in the Views sub-folder. – connectedsoftware Oct 17 '14 at 09:17
14

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>
Jian Huang
  • 1,065
  • 7
  • 13
3

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.

Luke Puplett
  • 36,042
  • 37
  • 161
  • 231
1

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

rajeemcariazo
  • 2,390
  • 5
  • 31
  • 58