zoukankan      html  css  js  c++  java
  • 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论坛的帖子

    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文件中。

    1   <ItemGroup>
    2     <Reference Include="System.Core" />
    3 ...

    真相大白了。原来这些系统自动引用的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。

  • 相关阅读:
    MVC4中Ajax.BeginForm OnSuccess 不执行以及控制器返回JsonResult 提示下载的原因
    string.Equals 比较2个字符串是否相同忽略大小写
    [转载]mvc使用JsonResult返回Json数据
    [转载]深入理解ASP.NET MVC之ActionResult
    [转载]自定义ASP.NET MVC Html辅助方法 TagBuilder
    [转载]AOP面向方面编程
    [转载]ASP.NET MVC 3的分部视图
    适配 iOS尺寸
    IOS Bug分析
    苹果一些乱七八糟流程整理
  • 原文地址:https://www.cnblogs.com/jiujiduilie/p/9113799.html
Copyright © 2011-2022 走看看