Ian Davis
Ian Davis

Reputation: 19423

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!

Upvotes: 31

Views: 13446

Answers (11)

Suresh Mahawar
Suresh Mahawar

Reputation: 1578

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.

Upvotes: 3

KNC
KNC

Reputation: 104

It looks Like We need to change the version number, if we upgrade to mvc4 from mvc3.

I just changed the webconfig, which is in Views Folder.... Need to change the version number

<configuration>
  <configSections>
    <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>
  </configSections>
<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>


 <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=**4**.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

Upvotes: 0

Sergio
Sergio

Reputation: 1423

The problem it's that you may have another web.config in the Views directory that have recerences pointing to the old asemblies. When the project loads, It tries to load both versions and then fails.

Try to also correct the versions on the "View's" web.config.

For the oncoming, like me.

Upvotes: 1

user1964763
user1964763

Reputation: 46

Might help someone:

  1. Added correct version of DLL conlfic between razor 1.0 and 2.0
  2. Modified the web.config file where it says 3.0 to 4.0 and 1.0 to 2.0 only for MVC and razor
  3. Another web.config hidden under view that also need to be modified.

Above three steps helped to resolve the issue.

Upvotes: 1

medvekoma
medvekoma

Reputation: 1179

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.

Upvotes: 6

reach4thelasers
reach4thelasers

Reputation: 26909

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

Upvotes: 59

Paulo Giron
Paulo Giron

Reputation: 161

Make sure that the references to System.Web.WebPages is 2.0.0.0 in root/Web.config and root/Views/Web.config

If every thing is pointing to version 2 then verify if you have the directory "_bin_deployableAssemblies".

If you have it, then right click on your project and choose the option "Add Deployable Dependencies..." then check MVC and Razor options to update your DLLs.

Upvotes: 1

Bobby D
Bobby D

Reputation: 2159

Just in case someone else runs into this same problem. It looks like there are a few different things that can cause this particular error. This is the best description of what is causing the error that I've found. In my specific case, I added a reference to a library via NuGet, and it looks as if a specific reference to System.Web.WebPages was added to my project. I removed this reference, rebuilt, and everything was good to go.

Good luck to others with similar problems!

Upvotes: 1

drogon
drogon

Reputation: 1825

When you install MVC4, your mvc3 projects will point to system.web.webpages ver 2.0..

For you MVC3 projects, Remove the reference to system.web.webpages ver 2.0.. and add a reference back to system.web.webpages ver 1.0..

Recompile and redeploy and it should work again...

Not sure why it does that, but it's still a beta install

Upvotes: 1

Johan Wikstr&#246;m
Johan Wikstr&#246;m

Reputation: 921

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!

Upvotes: 4

Travis J
Travis J

Reputation: 82287

That looks like you may have to update some of the assembly data in your web.config.

Upvotes: 0

Related Questions