c# - 无法加载文件或程序集 System.Web.Mvc 或其依赖项之一

标签 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-5

目前致力于 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.

网络配置:

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

最佳答案

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

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。它被解析为您的网络应用程序的 bin 文件夹下的那个:C:\Users\joaki\Source\Repos\2015-TimeAdministration\Source\V2\SalesWeb2\TimeReportV2\bin\System.Web.Mvc.DLL

接下来,它会检查您的 web.config 中是否存在该 DLL 的任何程序集绑定(bind)重定向,并找到一个:

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 文件夹,这也会有所帮助。

您也可以使用包管理器控制台来更新您的解决方案中的包:

Update-Package Microsoft.AspNet.Mvc -version 5.2.3.0

关于c# - 无法加载文件或程序集 System.Web.Mvc 或其依赖项之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782381/

相关文章:

c# - 嵌套的 foreach 循环读取 .xml 并将对象写入列表

c# - 使用C#在asp.net中填充DataTable时出现System.OutOfMemoryException错误

html - 在 div 中将 DropDownList 居中

asp.net - ASP.NET MVC如何在不离开当前页面的情况下从文件下载操作返回错误?

asp.net - 在 View 中显示经过身份验证和匿名的内容

.net - Nhibernate vb.net mvc 无法编译映射

c# - 使用 Visual Studio 内置的 ASP.NET MVC 模板

c# - 如果另一个命令成功则执行另一个命令?

c# - 如何在asp.net中拖动文本框时调整其大小?

javascript - 导出按钮 数据表