无法加载文件或程序集 System.Web.Mvc 或其某个依赖项

14

我目前正在处理一个asp.net mvc5 (旧的mvc3项目)。它可以成功构建,但当我启动项目并运行时,我遇到了以下错误。

Could not load file or assembly 'System.Web.Mvc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我不确定如何修复这个问题,有什么建议吗?

这是程序集加载跟踪信息:

=== Pre-bind state information ===
LOG: DisplayName = System.Web.Mvc
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Web.Mvc | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/joaki/Source/Repos/2015-TimeAdministration/Source/V2/SalesWeb2/TimeReportV2/
LOG: Initial PrivatePath = C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\web.config
LOG: Using host configuration file: C:\Users\joaki\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/joaki/AppData/Local/Temp/Temporary ASP.NET Files/root/a90c5137/552c494a/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Users/joaki/AppData/Local/Temp/Temporary ASP.NET Files/root/a90c5137/552c494a/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Users/joaki/Source/Repos/2015-TimeAdministration/Source/V2/SalesWeb2/TimeReportV2/bin/System.Web.Mvc.DLL.
LOG: Using application configuration file: C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\web.config
LOG: Using host configuration file: C:\Users\joaki\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.0.1 redirected to 5.2.3.0.
LOG: Post-policy reference: System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Users/joaki/AppData/Local/Temp/Temporary ASP.NET Files/root/a90c5137/552c494a/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Users/joaki/AppData/Local/Temp/Temporary ASP.NET Files/root/a90c5137/552c494a/System.Web.Mvc/System.Web.Mvc.DLL.
LOG: Attempting download of new URL file:///C:/Users/joaki/Source/Repos/2015-TimeAdministration/Source/V2/SalesWeb2/TimeReportV2/bin/System.Web.Mvc.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Web配置文件:

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
    <connectionStrings>
    <add name="SaleswebEntities" connectionString= ""/>
  </connectionStrings>

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" >
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </assemblies>
    </compilation>

  <authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2880" />
  </authentication>

  <membership>
    <providers>
      <clear />
      <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
    </providers>
  </membership>

  <profile>
    <providers>
      <clear />
      <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
  </profile>

  <roleManager enabled="false">
    <providers>
      <clear />
      <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
      <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
    </providers>
  </roleManager>

  <pages>
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      <add namespace="System.Web.Mvc.Html" />
      <add namespace="System.Web.Routing" />
      <add namespace="System.Web.Helpers" />
      <add namespace="System.Web.WebPages" />
    </namespaces>
  </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

</configuration>

看起来DLL引用和web.config中的程序集绑定之间存在版本不匹配的问题。请检查这些内容。 - Karthik
严重程度 代码 描述 项目 文件 行 警告 无法解析此引用。找不到程序集“System.Web.Mvc,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35,processorArchitecture = MSIL”。请确保磁盘上存在该程序集。如果您的代码需要此引用,则可能会出现编译错误。 Salesweb.Common C:\ Program Files(x86)\ MSBuild \ 14.0 \ bin \ Microsoft.Common.CurrentVersion.targets 1819看起来我需要 System.web.mvc 2.0.0.0 而不是我拥有的最新版本。 - Joakim Carlsson
4个回答

28

如果您仔细阅读您发布的融合日志,它会提供您所需的信息。 我将它简化为重要的行。

LOG: Attempting download of new URL file:///C:/Users/joaki/Source/Repos/2015-TimeAdministration/Source/V2/SalesWeb2/TimeReportV2/bin/System.Web.Mvc.DLL.

这个应用程序正在寻找 System.Web.Mvc.dll 文件。它已将其解析为位于您的 Web 应用程序的 bin 文件夹下的一个文件:C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\bin\System.Web.Mvc.DLL

接下来,它会检查您的 web.config 文件中是否有任何关于该 DLL 的程序集绑定重定向,并找到一个:

LOG: Using application configuration file: C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\web.config
LOG: Redirect found in application configuration file: 4.0.0.1 redirected to 5.2.3.0.
LOG: Post-policy reference: System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

如果你查看你的web.config文件,会有类似于这样的内容:
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>

然后它检查您应用程序的bin目录中是否找到System.Web.Mvc.dll,且版本号相同:

WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

提示主要版本不匹配,这是4位数字版本中的第一个数字。如果您回到日志中,那是因为bin文件夹中的System.Web.Mvc.dll实际上是版本4.0.0.1,而不是所需的5.2.3.0:

LOG: Redirect found in application configuration file: 4.0.0.1 redirected to 5.2.3.0.

您需要检查您的解决方案,确保所有引用 System.Web.Mvc 的项目都使用相同的最新版本。如果您查看错误列表中的警告,您很可能会发现有关此程序集的版本不匹配的警告。

更新引用

一种解决方案是右键单击 Visual Studio 中的解决方案,选择“管理解决方案的 NuGet 包...”并尝试更新项目以使用相同版本的 System.Web.Mvc。

在清空 bin 文件夹之前,这也会有所帮助。

您还可以使用 Package Manager 控制台跨解决方案更新包:

Update-Package Microsoft.AspNet.Mvc -version 5.2.3.0

那确实解决了MVC的问题..谢谢,但是它留下了另一个问题,我现在会尝试去解决。System.MethodAccessException: 安全透明方法“System.Web.Mvc.PreApplicationStartCode.Start()”尝试访问安全关键方法“System.Web.WebPages.Razor.PreApplicationStartCode.Start()”失败。 感谢您的帮助。 - Joakim Carlsson
这太复杂了...最终我决定用所有更新的NuGet开始一个新项目,然后将旧项目的所有代码转移到新项目中...虽然很长但很有效。 - Antoine Pelletier

2

我不确定这是否有帮助,但我也遇到了这个错误信息:

无法加载文件或程序集“System.Web.Mvc(1)”或其某一个依赖项...

我不太清楚原因,但当我检查 Web 应用程序的 bin 文件夹时,一些 DLL 文件有重复,如 "System.Web.Mvc(1).dll" 等等。

我的解决方法是删除所有重复的文件(文件名中带有“(1)”或“(2)”等),然后重新构建 Web 应用程序,它就可以正常工作了。


1
请问您能否在此处发布您的web.config文件?看起来您在新环境中打开了一个mvc3项目,但是您的IDE(即Visual Studio)的迁移功能不太好用。System.Web.Mvc.DLL的版本不匹配(在配置文件和bin/目录中)。有两种方法可以解决这个问题: 1. 修改配置文件中System.Web.Mvc包/程序集的版本。 2. 将System.Web.Mvc.dll从旧项目复制到新项目(bin/文件夹)。
我以前遇到过其他.dll文件的类似问题。那时,我选择删除所有.dll文件,并使用NuGet Package Manager重新下载它们。

我已经发布了我的Web配置文件,我会尝试你写的内容。 - Joakim Carlsson

0

您只需要将MVC更新到所需版本即可。在解决方案资源管理器中,右键单击“引用” -> 选择“管理Nuget程序包”管理Nuget,然后选择“浏览”选项卡 -> 搜索MVC -> 选择所需版本选择版本 -> 安装。

安装完成后,请尝试再次运行。这将解决此错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接