无法加载文件或程序集'System.Web.Mvc'.如何使用正确的参考?

Ahm*_*aba 18 .net asp.net asp.net-mvc asp.net-mvc-5

我有一个基于ASP.NET Web表单的应用程序,它也支持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>
Run Code Online (Sandbox Code Playgroud)

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

===预绑定状态信息===日志:DisplayName = System.Web.Mvc(部分)WRN:为程序集提供了部分绑定信息:WRN:程序集名称:System.Web.Mvc | 域ID:2 WRN:仅提供部分程序集显示名称时发生部分绑定.警告:这可能导致装订器装入错误的装配.警告:建议为程序集提供完全指定的文本标识,即WRN:由简单名称,版本,区域性和公钥标记组成.警告:请参阅白皮书 http://go.microsoft.com/fwlink/?LinkId=109270有关此问题的更多信息和常见解决方案.日志:Appbase = file:/// C:/ Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/ YuClone/YuClone/LOG:Initial PrivatePath = 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文件:/// C:/ Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL.日志:尝试下载新的URL文件:/// C:/ Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL .日志:尝试下载新的URL文件:/// 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文件:/// C:/ Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc.DLL.日志:尝试下载新的URL文件:/// C:/ Users/Ahmed/AppData/Local/Temp/Temporary ASP.NET Files/root/22a49c5f/2f5ffeb9/System.Web.Mvc/System.Web.Mvc.DLL .日志:尝试下载新的URL文件:/// C:/ Users/Ahmed/Documents/Visual Studio 2013/Projects/MediaSoft/Project/c#/ YuClone/YuClone/bin/System.Web.Mvc.DLL.警告:比较程序集名称导致不匹配:次要版本错误:无法完成程序集的设置(hr = 0x80131040).

以下是Stack Trace

[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的版本以指向正确的程序集?

Ahm*_*aba 12

错误是由于不正确的绑定引起的.我变了:

<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>
Run Code Online (Sandbox Code Playgroud)

  <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" />
Run Code Online (Sandbox Code Playgroud)

它现在有效.


Irf*_*Irf 8

好吧,这是版本绑定问题,对于像2019年一样现在开始使用VS 2017的用户我遇到过)和OP提到:

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

始终nuget package manager为您做,以根据项目.NET版本等安装正确的版本。

从那以后,我到达这里搜索我的问题:

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

并获得了有关版本绑定问题的指示,我执行了以下操作: Nuget Mvc更新

并且此后工作非常顺利。

@Ahmed,尽管感谢您指出了正确的方向


Mig*_*Slv 5

就我而言,我通过更改配置上的PageControlsassemblyBinding元素以匹配“System.Web.Mvc”的当前版本(即 5.2.4.0)来解决

页面控件

在该部分中,将所有出现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>
Run Code Online (Sandbox Code Playgroud)

装配装订

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>
Run Code Online (Sandbox Code Playgroud)

该元素由引用旧版本的任何包使用。

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

您可以在属性选项卡中看到当前版本System.Web.Mvc,从项目引用中选择程序集,如下所示:

在此输入图像描述

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