InvalidCastException is thrown after installing ASP.NET MVC 4 Beta

Solution 1:

I had the same problem, but when migrating ASP.NET MVC 3 to ASP.NET MVC 4. This way I got on this topic. I have found a solution to the problem, but its source is different, it is not from WebPages version.

I have followed the official guide (so to say) Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4.

To resolve the problem, you have to add in Web.Config (probably around previously added ones):

<runtime><!-- Should be there by default, near end -->
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><!-- Should be there by default -->
    <dependentAssembly>..</dependentAssembly><!-- Should be there by default -->
        ... some other dependecy redirects ...

    <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor"
            publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
    </dependentAssembly>

        ... some other dependency redirects ...
    </assemblyBinding>
</runtime>

Also in the notes is not mentioned to change the Reference of System.Web.Helpers from 1.0.0.0 to 2.0.0.0 (delete and add the new one).

Solution 2:

One more, rather late, solution ... I ran into this same problem. Did all the fixes listed here (Thank You All!!!) but could not get past the error. Finally, in desperation, I found a web.config file in the Views directory. In this file, I found several references to the version of System.Web.Mvc and bumped all of them up to "4.0.0.0". More importantly, I found the following lines and updated the version numbers for "system.web.webPages.Razor" from "1.0.0.0" to "2.0.0.0" and suddenly, everything was working once again. Hopefully, this will help someone else.

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

Steve G.

Solution 3:

Try adding the following in your web.config:

<appSettings>
  <add key="webpages:Version" value="1.0.0.0"/>
</appSettings>

The issue might be due to multiple build providers being registered and the latest winning.