Why am I getting 'One or more types required to compile a dynamic expression cannot be found.'?

I had a project that I've updated from

  • .NET 3.5 MVC v2 to
  • .NET 4.0 MVC v3

Compiling I get an error when I try to use or set the @ViewBag.Title property.

One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

I have done the following

  1. Followed the upgrade steps
  2. Set the target framework in the Project/Properties/Application tab to .NET Framwework 4
  3. Added the System.Core framework manually
  4. Added the Microsoft.CSharp framework
  5. Added the System.Xml framework
  6. Added the System.Xml.Linq framework
  7. Made sure my Web.Config is correct (it gets written over by Web.Debug.Config, etc)
  8. Created the model, controller and a Razor View
  9. Create a __Layout.cshtml_

I'm at loss as to what else to do?

EDIT:

I notice in the GAC Gui there is System.Core 3.5.0.0 but no System.Core 4.0.0.0. Does this mean that its getting confused and is still using the previous version? Or am I misunderstanding the GAC?

EDIT:

My web.config looks as follows

...
<compilation debug="true">
    <assemblies>
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    </assemblies>
</compilation>
....
<pages>
    <controls>
    <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</controls>
<namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.WebPages"/>
    <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.Linq" />
    <add namespace="System.Collections.Generic" />
</namespaces>
</pages>
....

For what it's worth, when this happened to me (in a project converted automatically from MVC2 to MVC3), all I had to do was add a project reference to Microsoft.CSharp. In my case the web.config already had the 4.0 references mentioned here, I assume courtesy of the upgrade script.


Okay I got it working with the following steps.

  1. In the config file I changed <compilation debug="true"> to <compilation debug="true" targetFramework="4.0"> in the system.web section
  2. In the config file I changed <providerOption name="CompilerVersion" value="v3.5" /> to <providerOption name="CompilerVersion" value="v4.0" /> in the compilers section
  3. In Global.asax.cs I added ViewEngines.Engines.Add(new RazorViewEngine()); to the Application_Start() method

and I think that was it.


You have to Add a Reference for Microsoft.CSharp.dll version - v4.0.30319 to the project. Which is avaiable at C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll, after istalling VS 2010


For me the solution was to add:

ViewEngines.Engines.Add(new RazorViewEngine());

to the Application_Start() method in global.asax.cs.