Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installed MVC 4 Beta, now my MVC 3 projects throw an error when I rebuild all

I just installed MVC 4 Beta, and now my MVC 3 projects (that I haven't converted yet to 4) are throwing an error when I rebuild all:

Error 18 [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=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'. Type B 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'.

Has anyone come across this yet? Thanks!

like image 654
Ian Davis Avatar asked Feb 21 '12 20:02

Ian Davis


4 Answers

I've spent the entire day trying to fix this, and after an afternoon systematically ripping EVERYTHING out of my project except one basic view and one basic controller it was still throwing this error. So I started deleting other stuff and found there was a rogue Web.Config file in my Views folder that had MVC 3 stuff in it.

Search your project for other Web.Config files!!!

like image 50
reach4thelasers Avatar answered Nov 14 '22 08:11

reach4thelasers


I had the same issue, and was able to solve it as follows:

  • Remove the assembly System.Web.WebPages.Razor from the project references.
  • Press Save All on the solution
  • Add the assembly again, make sure you select version 1.0.0.0. Select True for Specific Version in the reference properties.
like image 45
medvekoma Avatar answered Nov 14 '22 07:11

medvekoma


I got an solution for this:

You need to add this to your mvc 3 web.config:

    <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
    </configSections>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />

        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.WebPages" />

        <!-- Your namespace here -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>

And also copy the binfiles from

C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies

to your project binfolder

If you need deploy your mvc 4 project on a server that you dont have installed mvc 4 on you need to the same but change version from 1.0.0.0 to 2.0.0.0 in the section part.

Good luck!

like image 4
Johan Wikström Avatar answered Nov 14 '22 08:11

Johan Wikström


The steps to fix this issue

  1. First find all web.config files in whole project
  2. In all web.config file, find the given 'cannot be cast' error code and change version 2.0.0.0 in configSections of all web.config files System.Web.WebPages.Razor.Configuration.HostSection
  3. again if the error comes then find that error code and change the version 2.0.0.0 in all web.config files. repeat the process until fix the version related issues.
like image 3
Suresh Mahawar Avatar answered Nov 14 '22 07:11

Suresh Mahawar