asp.net-mvc 异常 – 无法加载文件或程序集“System.Web.Mvc,版本=4.0.0.1...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31134937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:28:37  来源:igfitidea点击:

Exception – Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.1…'

asp.net-mvc

提问by Tania Marinova

There is a problem – only when I request one single controller – I get the exception:

有一个问题——仅当我请求一个控制器时——我得到异常:

Could not load file or assembly 'System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040

无法加载文件或程序集“System.Web.Mvc,版本=4.0.0.1,文化=中性,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040

It's strange because in my config file I see that the version is 4.0.0.0 and and all the other controllers are fine and only when I upload the application on the server this thing happens.

这很奇怪,因为在我的配置文件中,我看到版本是 4.0.0.0,并且所有其他控制器都很好,只有当我将应用程序上传到服务器时才会发生这种情况。

回答by staticvoidmain

This is a known issue. Seems you have migrated recently from 3.0 to 4.0 or this application is a fresh clone from the repository. The one single controller may not have nuget restored. I have also faced this issue. I got this msdn linkand browsing it had a solution. Here are the steps mentioned.

这是一个已知的问题。似乎您最近从 3.0 迁移到 4.0,或者此应用程序是存储库中的新克隆。一个控制器可能没有恢复 nuget。我也遇到过这个问题。我得到了这个 msdn链接并浏览它有一个解决方案。以下是提到的步骤。

The problem can be resolved by implementing one of the following solutions:

  1. (Preferred) Install Microsoft.AspNet.Mvc from the NuGet gallery (this will install a binding redirect in your web.config). You can do this from the NuGet package manager or the NuGet console inside Visual Studio:

    Install-Package Microsoft.AspNet.Mvc -Version -Project PROJECTNAME

    MVC 4 version: 4.0.40804.0

    MVC 3 version: 3.0.50813.1

  2. Manually update the reference to System.Web.MVC.dll (don't use the one in the GAC).

    Try the Add Reference -> Assemblies -> Extensions dialog box.

In either case ensure that the Copy Local project property for the assembly is set to true so it ends up in your bin folder which is needed for deployment. There is a known NuGet bug that resets the Copy Local flag: https://nuget.codeplex.com/workitem/4344

可以通过实施以下解决方案之一来解决该问题:

  1. (首选)从 NuGet 库安装 Microsoft.AspNet.Mvc(这将在您的 web.config 中安装绑定重定向)。您可以从 NuGet 包管理器或 Visual Studio 中的 NuGet 控制台执行此操作:

    安装包 Microsoft.AspNet.Mvc -Version -Project PROJECTNAME

    MVC 4 版本:4.0.40804.0

    MVC 3 版本:3.0.50813.1

  2. 手动更新对 System.Web.MVC.dll 的引用(不要使用 GAC 中的引用)。

    尝试添加引用 -> 程序集 -> 扩展对话框。

在任一情况下,请确保将程序集的 Copy Local 项目属性设置为 true,以便它最终位于部署所需的 bin 文件夹中。有一个已知的 NuGet 错误会重置复制本地标志:https: //nuget.codeplex.com/workitem/4344

Install Nuget Package Microsoft.AspNet.Mvc for all the project referencing System.Web.Mvc dllHope this solves your problem

为所有引用 System.Web.Mvc dll 的项目安装 Nuget Package Microsoft.AspNet.Mvc希望这能解决您的问题

回答by Andre Luus

I got this build error on a VSO agent thanks to the ASP.Net assembly resolve weirdness. I had changed some of my build configurations and Visual Studio modified the output directory for the MVC project to \bin\x86\Debugor something similar.

由于 ASP.Net 程序集解决了奇怪的问题,我在 VSO 代理上遇到了这个构建错误。我更改了一些构建配置,Visual Studio 将 MVC 项目的输出目录修改为\bin\x86\Debug或类似的内容。

This had bitten me before and I remembered ASP.Net wants to be built to \binONLY.

这之前曾让我感到困惑,我记得 ASP.Net 想要构建为\binONLY。

So when you get any kind of "reference could not be found" on build time, remember to check that your build output directory is only \bin:

因此,当您在构建时遇到任何类型的“找不到引用”时,请记住检查您的构建输出目录是否仅为\bin

enter image description here

在此处输入图片说明

More on the problem here: Changing output directory for asp.net project in Visual Studio

更多关于这里的问题: Changing output directory for asp.net project in Visual Studio

Hope this helps someone.

希望这可以帮助某人。

回答by Tomas Kubes

You maybe can reference ASP.NET MVC as reference to GAC, but it can happens that different developers have different version of MVC installed.

您也许可以将 ASP.NET MVC 引用为对 GAC 的引用,但可能会发生不同的开发人员安装了不同版本的 MVC。

Remove references to ASP:NET MVC and add ASP.NET MVC as nuget package.

删除对 ASP:NET MVC 的引用并将 ASP.NET MVC 添加为 nuget 包。

回答by César León

Go to Nuget manager, search for: "Microsoft.AspNet.Mvc", and press update button.

转到 Nuget 管理器,搜索:“Microsoft.AspNet.Mvc”,然后按更新按钮。

That work on my case.

这对我的案子有效。

回答by Neo

I got this also at run-time, and it was due to my project (a unit test project using NuGet PackageReference) referencing an ASP.NET website project that already had the necessary binding redirects in place (as suggested by the compiler warnings when building). I had to add the same binding redirect in the unit test project.

我也在运行时得到了这个,这是由于我的项目(一个使用 NuGet PackageReference 的单元测试项目)引用了一个 ASP.NET 网站项目,该项目已经有必要的绑定重定向(如构建时编译器警告所建议的那样) )。我不得不在单元测试项目中添加相同的绑定重定向。

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" culture="neutral" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>