23

Working against the current RC2 - the template that is generated Razor views includes:

@{
    ViewBag.Title = "Details1";
    Layout = "/Views/Shared/_Public.cshtml";

}

With a red squiggly under ViewBag.Title and this compiler error:'

Error   4   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? c:\Visual Studio 2010\Projects\myProj\Views\Webinar\Details1.cshtml 6   2   TTSTrain.Webinars.WebEntry

But the project builds and functions correctly. Is the error indicative of other problems that should be addressed?

skaffman
  • 398,947
  • 96
  • 818
  • 769
justSteve
  • 5,444
  • 19
  • 72
  • 137

9 Answers9

16

I got the same problem after I removed the targetFramework attribute from the <compilation> element in the Web.config file.

Once I restored it to

<compilation debug="true" targetFramework="4.0">

Everything worked fine again!

Peter
  • 14,221
  • 15
  • 70
  • 110
  • 5
    I have the problem and checked and I already have that line :( – Tom Stickel Sep 01 '11 at 07:11
  • @Tom Stickel: have you seen this post? http://stackoverflow.com/questions/7115055/why-am-i-getting-one-or-more-types-required-to-compile-a-dynamic-expression-cann – Peter Sep 01 '11 at 07:24
  • @Peter: Thanks I still haven't fixed mine yet, but some useful things to try in that link, thanks for that. With deadlines and constantly checking in and out files with a new developer with TFS, it started happening. It seems that service reference updates to a WCF service would really mess up my web.config. It may be the web.config, but I have been using Nuget Powershell a lot. – Tom Stickel Sep 01 '11 at 07:41
  • @Tom regarding the WCF service references, we don't use the provided UI tools. We just create a dll using svcutil. This is then the proxy dll to the service, which we reference in our project and then we add the bindings and client endpoints to the webconfig. This way Visual Studio doesn't mess around with the web.config as much. But thats irrelevant to your problem probably. I'm sorry I can't offer you any more pointers to this problem. – Peter Sep 01 '11 at 07:54
  • @Peter: For now as long as it compiles (it does) and I can make deadlines and deploy the staging servers at it works (it does) that is good for now. It may or may not be a complex issue. I will have to shelf it for now until more time... :) thanks! – Tom Stickel Sep 01 '11 at 17:21
  • Now this is odd. In VS2012 the same thing was happening. Removed "targetFramework="4.0" then compile. Added targetFramework="4.0" compile for second time and the annoying message is gone. Thanks anyway. – coffekid Jun 04 '13 at 21:09
9

I solved it in the following way:

First i noticed using gacutil (Global Assembly Cache Utility) that it contained two references to System.Core, one to version 4.0 and one to version 3.5. Apparently inside the razor views, even if in the project i had the correct reference to version 4.0, it was still using version 3.5 and that's why i was getting the error about the dynamic types. To check if that's your case open as administrator Visual Studio Command Prompt and execute:

gacutil -l System.Core

To remove the reference to the old version of System.Core i did the following steps:

- cd %systemroot%\assembly\

From here you may have more that one "gac" directory, so you will have to search within each to find your component. For me, it was within the "gac_MSIL" directory.

- cd gac_msil
- cd System.Core
- cd <assembly version number>__<public key token>
- erase *.* Say "y" to are you sure.
- cd ..
- rd <assembly version number>__<public key token>
- cd ..
- rd System.Core

After that I opened my solution again in visual studio and the error was gone, it references properly to System.Core 4.0 and I was not getting the dynamic errors anymore :)

I hope it will help you as well, Best, N.

kefer9
  • 334
  • 4
  • 7
4

Similar to @Kaiser's answer, I experienced this problem as a result of having multiple System.Core assemblies in the GAC.

I chose not to delete the 3.5 assembly, however. Instead, in the Views web.config, I modified the configuration/system.web/compilation node as follows:

<compilation debug="true" targetFramework="4.0">
    <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.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=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </assemblies>
</compilation>

The important line is the last assembly node: it tells the Razor compiler which GAC assembly version to use.

Once I did this, all was well in my Razor views.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
2

I do not have this problem when running VS 2012 as administrator.

Otherwise, what worked for me:

in root web config have added as recommended reference to correct assembly as child of compilation node`

<system.web>
    <compilation debug="true" targetFramework="4.5">
        <assemblies>
            <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </assemblies>
    </compilation>
</system.web>

set copy local = true properties for System.Core and Microsoft.CSharp`

JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
algor77
  • 191
  • 4
1

By using Peters answer i managed to solve the issue with Html.EditorFor(m => m.xxx) underline errors in the chtml files. Althought the ViewBar error persisted. So i changed the web.config like this

<compilation debug="true" targetFramework="4.5.1">
  <assemblies>
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add assembly="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </assemblies>
</compilation>

Notice the Microsoft.Csharp line. The above did the trick and now the chtml editor is clear from the red underlines. Thanks Peter

PanKak
  • 133
  • 12
1

Do you have a reference to Microsoft.CSharp and System.Core?

MVC Views (usually) get compiled dynamically when you access your site, not when you compile the application in VS. I imagine you will see issues when running the site. Just add the two references and you should be fine.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • 1
    Microsoft.CSharp is in the list of references but System.Core is not. When I bring up the 'Add Reference' dialog System.Core is checked but doesn't respond to 'Remove' (hoping to re-add it). – justSteve Jan 05 '11 at 19:16
  • I included those assemblies, but it didn't solve the problem. – Jonathan May 03 '11 at 10:49
  • Everthing is working, I just do not want the red-line under the `ViewBag` and make it looks like error, very annoying, how to disable it? – Eric Yin Aug 10 '12 at 17:44
0

I had the exact same problem. By default, when you create an MVC3 app it sticks a web.debug.config and a web.release.config in the solution. When I got rid of those two items, the ViewBag issue resolved itself. It may have something to do with what Peter was saying above but I didn't test that.

Perry
  • 611
  • 6
  • 15
-1

That is the time when other fields in ViewBag is read. So if you are passing them from controller. ViewBag.yourobjectDto = yourObjectDto;

make sure this line is not blocked through if condition or something.

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
-2

Try

Page.Title = "Details1";

It might work.

Emil
  • 8,449
  • 3
  • 27
  • 44