无法加载文件或程序集“System.Web.Mvc”。如何使用正确的引用?

28
我有一个基于ASP.NET Web Forms构建的应用程序,同时支持MVC 5。我在我的项目中包括了MVC 5 API控制器,为此我必须将Json从版本4.5升级到7.0。在做出这些更改后,当我启动应用程序时,我会收到以下错误:
“未能加载文件或程序集'System.Web.Mvc'或其某个依赖项。所定位的程序集清单定义与程序集引用不匹配(来自HRESULT的异常:0x80131040)”。
我检查了“System.Web.Mvc”的参考文献。在属性对话框中,运行时版本为“v4.0.30319”,版本为“5.0.0.0”。在web.config中,程序集设置如下:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.0.1.0" newVersion="2.0.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="AntiXssLibrary" publicKeyToken="d127efab8a9c114f" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
  </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>
  <!--<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
  </dependentAssembly>-->
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
</assemblyBinding>

完整的错误日志如下所示:

=== 预绑定状态信息 === 日志:显示名称 = System.Web.Mvc(部分) 警告:为程序集提供了部分绑定信息: 警告:程序集名称:System.Web.Mvc | 域 ID:2 警告:当仅提供程序集显示名称的一部分时会发生部分绑定。 警告:这可能导致绑定器加载错误的程序集。 警告:建议为程序集提供完全指定的文本标识, 警告:其中包括简单名称、版本、区域性和公钥令牌。 警告:有关此问题的更多信息和常见解决方案,请参见白皮书 http://go.microsoft.com/fwlink/?LinkId=109270 。 日志:应用程序基础目录 = file:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/ 日志:初始私有路径= C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\bin 调用程序集:(未知)。 === 日志:此绑定在默认加载上下文中开始。 日志:使用应用程序配置文件:C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\web.config 日志:使用主机配置文件:C:\Users\Ahmed\Documents\IISExpress\config\aspnet.config 日志:使用机器配置文件:C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config。 日志:此时未将策略应用于引用(私有、自定义、部分或基于位置的程序集绑定)。 日志:尝试下载新 URL file:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL。 日志:尝试下载新 URL file:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL。 日志:尝试下载新 URL file:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/bin/System.Web.Mvc.DLL。 日志:使用应用程序配置文件:C:\Users\Ahmed\Documents\Visual Studio 2013\Projects\MediaSoft\Project\c#\YuClone\YuClone\web.config 日志:使用主机配置文件:C:\Users\Ahmed\Documents\IISExpress\config\aspnet.config 日志:使用机器配置文件:C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config。 日志:在应用程序配置文件中找到重定向:5.0.0.0 重定向到 5.2.3.0。 日志:策略后引用:System.Web.Mvc,Version=5.2.3.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35 日志:尝试下载新 URL file:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL。 日志:尝试下载新 URL file:///C:/Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL。 日志:尝试下载新 URL file:///C:/Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/YuClone/YuClone/bin/System.Web.Mvc.DLL。 警告:比较程序集名称导致不匹配:次要版本 错误:无法完成程序集的设置(hr = 0x80131040)。 探测终止。

以下是堆栈跟踪

[FileLoadException: 无法加载文件或程序集 'System.Web.Mvc' 或其其中一个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040)] [FileLoadException: 无法加载文件或程序集 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 或其其中一个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040)] System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0 System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark & stackMark, Boolean forIntrospection) +21 System.Reflection.Assembly.Load(String assemblyString) +28 System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38
[ConfigurationErrorsException: 无法加载文件或程序集 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 或其其中一个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040)] System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +738 System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +217 System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130 System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170 System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +92 System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +290 System.Web.Compilation.BuildManager.ExecutePreAppStart() +157 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +531
[HttpException (0x80004005): 无法加载文件或程序集 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 或其其中一个依赖项。所定位的程序集清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040)] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9924184 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +261

我该如何升级 System.Web.Mvc 的版本并指向正确的程序集?

8个回答

20

由于错误的绑定导致了错误。我进行了更改:

<dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>

to
  <dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.0.0.0" />

现在它可以正常工作。


19

这个问题涉及到的是版本绑定问题。对于那些像VS 2017这样的用户(就像我遇到的一样),以及正如OP所提到的:

如何升级System.Web.Mvc版本以指向正确的程序集?

始终让nuget包管理器为您完成此操作,以根据您的项目.NET版本安装正确的版本等。

由于我在搜索我的问题时找到了这里:

无法加载文件或程序集“System.Web.Mvc,Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其中之一的 依赖项。位于的程序集清单定义不是 匹配程序集引用。(HRESULT 的异常:0x80131040)

并得到了版本绑定问题的提示,我执行了以下操作:Nuget Mvc Update

之后,它运行得很顺利。

@Ahmed, 感谢你指出正确的方向,虽然


5
在我的情况下,我通过更改配置文件中的PageControlsassemblyBinding元素来解决问题,使其与当前版本的`System.Web.Mvc(5.2.4.0)相匹配。

PageControls

PageControls节中,将所有程序集"System.Web.Mvc"的版本替换为您当前的版本:

 <pages controlRenderingCompatibilityVersion="4.0" validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc"/>
  </controls>

assemblyBinding

assemblyBinding元素用于将所有先前版本的程序集映射到当前版本,如下所示:

 <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
  </dependentAssembly>

这个元素被任何引用旧版本的包使用。

如何检查“System.Web.Mvc”的当前版本

您可以在属性选项卡中查看System.Web.Mvc的当前版本,选择项目引用中的程序集,就像这样:

enter image description here

注意:我正在回答一个两年前的问题,所以现在出现此错误的原因可能不同。


1
我只需要添加程序集绑定条目 - 在我的情况下是版本5.2.7.0。 - daveywc

0

从NuGet包更新System.Web.Mvc的版本,它将正常工作。


对我来说没有解决问题。 - niico

0
在我的情况下,该应用程序在本地运行良好,但在其中一个服务器上却无法正常工作。移除“System.Web.Mvc”部分为我解决了这个问题。

0
只需从工具菜单中的NuGet Packages卸载并安装Microsoft.AspNet.Mvc即可。

this.


0

当我尝试发布我的解决方案时,我发现了这个错误信息。

问题的原因是'System.Web.Mvc'引用版本与我的'web.config'版本不相似。

在我的情况下,在'web.config'中引用版本为5.2.4.0,但在项目中为5.2.7.0。

要解决此错误 -

  1. 进入您的项目'References',然后找到'System.Web.Mvc'
  2. 右键单击以查看属性。您将获得'System.Web.Mvc'版本号。
  3. 打开您的'web.config'文件并转到'system.web'代码块,您将在那里找到'System.Web.Mvc'版本。
  4. 如果您的web.config程序集版本与项目'References'版本不匹配。
  5. 从项目'References'中删除'System.Web.Mvc'。
  6. 重新安装'System.Web.Mvc',并确保您安装的版本与您的web.config程序集版本相同。 示例包管理器控制台命令以安装Microsoft.AspNet.Mvc。运行命令时,您需要放置所需的版本号-

Install-Package Microsoft.AspNet.Mvc -Version 5.2.4.0

我不知道它是否适用于你,但对我来说是有效的。谢谢


-1
Ahmed Mujtaba的解决方案对我有用,当我将newVersion(回)更改为4.0.0.0时。

这并没有回答问题。一旦你有足够的声望,你就可以评论任何帖子;相反,提供不需要询问者澄清的答案- 来自审核 - lee-m

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