The type 'Expression<>' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
在我将一个.net framework 4.0+mvc4+ef5的项目,升级到.net framework 4.6.1+mvc5+ef6之后,解决了所有的升级带来的问题,唯独在razor的cshtml页面中,所有关于Expression<>的引用的方法全都报错,例如System.Web.Mvc.Html空间下的一系列@Html.DisplayFor()等等。razor页面可以正常编译及运行,但是页面编辑时报错,智能感知也不能正常使用。 具体的提示就是“The type 'Expression<>' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.”。
一、问题描述
在我将一个.net framework 4.0+mvc4+ef5的项目,升级到.net framework 4.6.1+mvc5+ef6之后,解决了所有的升级带来的问题,唯独在razor的cshtml页面中,所有关于Expression<>的引用的方法全都报错,例如System.Web.Mvc.Html空间下的一系列@Html.DisplayFor()等等。razor页面可以正常编译及运行,但是页面编辑时报错,智能感知也不能正常使用。
具体的提示就是“The type 'Expression<>' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.”。
翻译过来就是“'Expression<>'类型在未被引用的程序集中定义。你必须添加对'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'程序集的引用。”。
查询了baidu和google,网上的解决方案几乎千篇一律,甚至包括微软asp.net论坛的帖子
回答无非是nuget管理,web.config添加<dependentAssembly>,或项目引用之类的方案,经过尝试全都无效。
二、尝试解决
通过nuget根本管理不到System.Core这个基础dll。
通过项目引用添加也无济于事,如下图:

这个提示,启发了我。
“生成系统自动引用”,那么具体是在哪里管理配置的呢?无非就是web.config,.csproj这两个项目配置文件了。
看来是框架版本升级过程中,把system.core的引用搞丢了。
三、解决方案
重新建了一个mvc5项目,在web.config和.csproj两个文件中搜索system.core,结果发现确实是在.csproj文件中。
<ItemGroup>
<Reference Include="System.Core" />
...
真相大白了。原来这些系统自动引用的dll不在nuget/项目引用/web.config中管理,而是直接写在.csproj项目文件中。
于是,右键项目,卸载项目,编辑.csproj,加入system.core的引用<Reference Include="System.Core" />,重新加载项目。
再看cshtml视图页面,全都恢复正常了,问题解决。
四、补充(20190716)
一位同事在将系统改为Mono移植Linux的版本后,又出现了上面的问题,上面的方案解决无效,后来发现是web.config中的设置被修改了,少了配置 targetFramework="4.6.1"。
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
compilation是编译设置,在部署环境无须设置 targetFramework,debug 也可以设置成 false,但是开发环境需要设置targetFramework为对应版本,且debug为true。
顺带说下httpRuntime,httpRuntime是运行时,.Net 4.0 和 .Net 4.6.1 的运行时版本都是 .Net 4.0,所以改成4.0也可以,当服务器IIS只注册了.Net 4.0时,可以这样设置,以运行.Net 4.6.1的App。
--End
The type 'Expression<>' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.的更多相关文章
- Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...
- System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...
- Could not load file or assembly 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
项目升级后提示错误 System.IO.FileNotFoundException: Could not load file or assembly 'System.Windows.Forms, Ve ...
- 解决方法:Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.
最近使用VS2015调试ASP.NET 程序遇到了该问题: 在网上找了很多方法都不能解决,最后自己解决了,方法如下: 在project -> NuGet管理中找到已安装的所有程序:将Web Op ...
- ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0
ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...
- Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c
错误: Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, V ...
- Type 'Insus.NET.PictureObject' in Assembly 'App_Code, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
昨晚想实现一个功能,需要把一个对象存储于ViewState中去,但在运行时,出现下面的异常. Type 'Insus.NET.PictureObject' in Assembly 'App_Code, ...
- TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.Internal.IHttpResponseStreamWriterFactory' from assembly 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.2.0 ...
今天调试 asp.net core 2.0 项目时遇到了如下错误: TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.I ...
- Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...
最近老大让我看Orchard Core,这是一个CMS系统.可以先参考大佬的文章:https://www.cnblogs.com/shanyou/archive/2018/09/25/9700422. ...
随机推荐
- fhq_treap 小结
简介 \(fhq\_treap\)是一种非旋平衡树.在学习这篇文章之前,还是先学习一下普通\(treap\)吧 优点 相比于普通的\(treap\),它可以处理区间操作. 相比于\(splay\),它 ...
- [APIO2017]商旅(floyd+分数规划+SPFA)
题解:首先肯定要跑最短路,而n<=100,所以可以用floyd,然后根据比值,很容易想到二分答案,然后再SPFA跑一遍负环,就能求出解了. #include<bits/stdc++.h&g ...
- C# 中的#if、#elif、#else、#endif等条件编译符号
C#编译器遇到一个由#if和#endif包围起来的语句块时,会检查#if后面的符号是否已经被定义了,如果已经被定义,那么才会编译语句块之间的代码.而定义一个可以被#if测试的符号需要事先用#defin ...
- 011 Socket定义客户端
引入命名空间: using System.Net; using System.Net.Sockets; using System.Threading;
- Git复制已有分支到新分支开发
如果我们需要在现有的分支代码基础上,复制代码到新分支进行开发,并推送至远程仓库,可以进行如下操作: 注:被复制的分支代码(ibis35),创建新的分支(ibis35-dev) 1. 切换到被copy的 ...
- 将图片转为base64
DEMO: <input type="file" id="file" multiple="multiple"> <div ...
- docker学习------swarm集群虚机异常关机,node状态为down
1.因昨天虚机异常关闭,导致今天上去查看时,node节点状态显示为down 2.查了些相关资料,找到处理办法(因我的节点没有任何数据,所以直接对其进行清除) docker swarm leave -- ...
- Contest2178 - 2019-4-18 高一noip基础知识点 测试7 题解版
刚刚改完题,才有时间发题解 传送门 T1 exgcd裸题 对a,b跑exgcd,答案就是x*c/gcd(a,b),y*c/gcd(a,b) 不合法的情况:当且仅当c%gcd(a,b)!=0 代码 T2 ...
- Groovy中的GString
在讨论GString之前,我们先讨论一下Groovy里面的String.在Groovy里面String有 println 'test string' println '''test string''' ...
- python面试题一个字符串是否由重复的子字符串组成
一,给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 输入: "abab" 输出: True 解释: 可由 ...