NET MVC 升级到5.1后,View视图中代码报错

使用nuget将项目中MVC4 升级到MVC5,之后项目还可以正常编译运行,

但View视图中相关的很多代码都报错,比如:

1.@model找不到

2.@Html找不到,本该是System.Web.MVC的HtmlHelper,系统却将其认成是

System.Web.WebPages.HTML.HtmlHelper

参考地址 http://stackoverflow.com/questions/4605263/viewbag-title-error

解决方法及原因:System.Core冲突,直接将3.5版本的System.Core删掉,只保留4.0的即可

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.

我运行的时候,提示我没有权限删除,然后在web.config里面添加

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

就可以解决,写上以此备忘。

 还有救是System.Web.Mvc 这个文件复制到本地

ViewBag 出现 找不到编译动态表达式所需的一种或多种类型。是否缺少引用?

需要引用 Microsoft.CSharp

原文地址:https://www.cnblogs.com/wudiliujie/p/4350843.html