在我将一个.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论坛的帖子

https://forums.asp.net/t/2137945.aspx?VS2017+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+

回答无非是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'.的更多相关文章

  1. 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, ...

  2. 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' ...

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

  4. 解决方法: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 ...

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

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

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

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

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

随机推荐

  1. [SDOI2017]遗忘的集合

    [SDOI2017]遗忘的集合 综合了很多套路的题 一看就是完全背包 生成函数! 转化为连乘积形式 Pi....=F 求Ln! 降次才可以解方程 发现方程是: f[i]=∑t|i : bool(t)* ...

  2. 使用Zabbix监控mysql的主从同步

    Zabbix 监控触发器设置 简述 在生产环境中,有一台mysql的备份服务器,上面运行着三个数据库实例的从库,也在做日志的同步工作,为了实现对该备份服务器的监控,当出现从库实例不为3或者日志同步进程 ...

  3. BAT面试经验分享——iOS高级开发工程师的自我总结!

    序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了. 今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,随着各大公司秋招的开始,很多小伙伴都行动起来了,我也有幸获得了一份不 ...

  4. Ubuntu18安装Pycharm时遇到的几个问题

    今天给电脑安装了Ubuntu18.04,安装了一些基础软件. 这里是Ubuntu安装Pycharm的一些问题: 1.安装Pycharm. 首先需要到Pycharm官网进行下载安装包,由于我的是Ubun ...

  5. 一个网站SQL注入的案例

    网站的页面提交参数做了md5转换,而且参数会带入两个SQL语句中执行. 注入是肯定存在的,但是SQLMAP怎么都跑不出来(可能原因是其中有个SQL语句总是报错). 尝试手工,发现 order by 报 ...

  6. docker学习------registry可视化的实现

    1.Harbor的发现 之前一直在使用registry,但docker  pull下来的基础镜像并没用携带UI界面,所以查找了一波开源工具,发现了Harbor这一工具,下面对工具进行一个学习. 2.部 ...

  7. AE二次开发中几个功能速成归纳(符号设计器、创建要素、图形编辑、属性表编辑、缓冲区分析)

    /* * 实习课上讲进阶功能所用文档,因为赶时间从网上抄抄改改,凑合能用,记录一下以备个人后用. * * ----------------------------------------------- ...

  8. 浅入深出Vue:环境搭建

    浅入深出Vue:环境搭建 工欲善其事必先利其器,该搭建我们的环境了. 安装NPM 所有工具的下载地址都可以在导航篇中找到,这里我们下载的是最新版本的NodeJS Windows安装程序 下载下来后,直 ...

  9. Selenium for C#(一) 环境安装

    Selenium 环境安装 本地环境为VS2015,由于selenium 官网不知什么原因打不开. 特记录下VS上使用NuGet安装Selenium的步骤. 利用Package Manager Con ...

  10. phpinfo

    phpinfo是一个运行指令,为显示php服务器的配置信息.    phpinfo函数是PHP最为常用的配置输出函数.phpinfo函数能够输出服务器PHP当前状态的大量信息,其中包含了PHP的编译选 ...