开发者

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

开发者 https://www.devze.com 2023-03-29 07:34 出处:网络
I had a project that I\'ve updated from .NET 3.5MVC v2to .NET 4.0 MVC v3 Compiling I get an error when I try to use or set the @ViewBag.Title property.

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" />
      开发者_Python百科  <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.


It looks like you are importing System.Core 3.5 in your web.config. You need to import System.Core from the 4.0 framework, because ViewBag requires the System.Dynamic namespace.

On my machine, it looks like System.Core v4.0.30319


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

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

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


This was my resolution:

1) Go to References of the project

2) Right click on the Microsoft.CSharp -> Properties

3) Change the Copy Local to False if you have it to True.


If you see that message in your MVC views, you need to add a reference in the Views\web.config like this:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        (... other references ...)
        <add namespace="Microsoft.CSharp" />  <-- Add this
        (...)
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Don't forget to do a build, otherwise the changes will not take affect.


A default project in Visual Studio 2010 should have references to Microsoft.CSharp and System.Core.

If they are not referenced in your project you need to add them, and the errors will go away.


Woow!

I am almost a year late but still think I should answer based on my findings (on July 13, 2013 at 7:15p PST) related to the error message. This might be helpful for the guys who are looking for the answer to the error message:

"One or more types required to compile a dynamic expression cannot be found."

so please read on: I had the similar error message when I implemented System.Dynamic in my code. The Web application runs smoothly as I had the correct configuration in web.config (targetframework) but when I put the app_code in a separate dll then it started to throw errors at compile time. Please note that I did not have app.config in my dll so it may NOT be dependent on configuration when you have it in a separate dll project but the reference to the correct dll.

I figured correct Microsoft.CSharp.dll was in "C:\Windows\Microsoft.NET\Framework\v4.0.30319", and my dll compiled successfully.


For me non of the above worked, instead setting Copy Local = True solved the issue


I had this problem when I updated the MVC version in my project. The problem was the assemblyBinding directive, somehow Nuget forgot to update it.

I had to change

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>

to

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>


I received this error for a windows app while using Microsoft.Office.Interop.Excel;...... runtime versions do not seem to be compatible between one or more DLLs.

MY SOLUTION: I converted my entire solution from .NET 4.0 to .NET 3.5

for the hell of it I converted back to 4.0 and I started receiving the error again.

based on this for your error try converted your solution to an older .NET framework that could work.

========== EDIT =================

2 hours later I finish up some key things in my Excell interop class and it works like a charm...for some reason only on .NET 3.5 and not 4.0 my solution is valid to this extent


None of the above worked for me. With more searching and looking through the Web.Config settings, I found this answer worked for me.

How do I fix the "compilerVersion" IIS error?


I had something very similar happen to me, although in my case I had upgraded a number of things including from Visual Studio 2010 up to Visual Studio 2012. I'd followed everyone of the above suggestions and still was suffering. In the end I decided to re-create the entire project. It was only when I tried to re-add the membership provider aspects to my web.config file that it suddenly became obvious where the versioning clash was for me.

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <clear/>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/AppName"/>
  </providers>
</sessionState>

Although looking at it now, its obvious. The type for the provide has a specific version. I simple then removed the version and the appropriate type is found. The resolution therefore in my case was to replace the above with:

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <clear/>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers" connectionStringName="DefaultConnection" applicationName="/AppName"/>
  </providers>
</sessionState>

And then re-compile. It has taken me days to find this, as I was looking in all the wrong places!


I tried all the other suggestions in this file and none worked on my existing MVC 4 solution. So after updating my VS2012 to WebTools 2013.1 for VS 2012, I created a new MVC 5 project and using the new project's files as a base, removed all extraneous lines from all my web.config files.

After doing that all my compilation errors and warnings disappeared and Intellisense started working correctly. Completely removing the <system.web><pages> section in the main web.config file finally fixed the ViewBag.Title (and other) dynamic expression errors.


As I recently found out, some extensions can also cause this. In my case removing AutoHistory 1.0.1 extension fixed it for me (MVC5).


One common cause of this problem is that you've missed updating an Assembly version in your Views/web.config file. It's pretty easy for me to miss them in <system.web><pages> for instance:

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


I've been struggling with this issue also for a day or so. After I finally found this article here and tried all the suggestions here I came to another solution that solved the Problem for me:

It's just the opposite as Arman Bimatov has found:

1) Go to References of the project

2) Right click on the Microsoft.CSharp -> Properties

3) Change the Copy Local to True if you have it to False.

I think it must deal somehow with the assemblies registered in GAC and the way Visual Studio gets the "right" Version. I guess on my developer machine somehow the .NET Framework 3.5 was installed after the newer Frameworks (if this is possible at all), so Visual Studio gets the wrong Version from GAC. Or simpply it is unclear for VS to get which Version, so it takes the older one...

If you still have same error message you can also try to set the Copy Local to True for the System.Core Dll and have a look it works now.

Regards SkippingAdi


I've been struggling with this issue for days and finally figured it out.

All I did was remove the following from the web.config:

<dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>


I start getting the same error after I installed MVCSiteMapProvider package to make breadcrumbs for my site.

If you've installed MVCSiteMapProvider and you get this error you need to replace @ViewBag.Title by something else, for example, @Html.MvcSiteMap().SiteMapTitle()

0

精彩评论

暂无评论...
验证码 换一张
取 消