安装CORS后无法加载文件

我使用所有默认值在VS Express 2013中创建了一个基本的ASP.NET Web Api应用程序。 我添加了一个控制器,它就像我想要的那样返回XML。

一旦我安装CORS包:

Install-Package Microsoft.AspNet.WebApi.Cors -Pre 

我甚至不能再运行该应用程序了:

 An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary Additional information: Could not load file or assembly 'System.Web.Http, Version=5.0.0.0, 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) 

我按照本博客文章中的所有说明进行了操作: http : //www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api ,对此问题一无所知。

我尝试使用以下方法更新Web API:

 Install-Package Microsoft.AspNet.WebApi -Pre 

但不幸的是,这个问题仍然存在

在项目中,我使用WebApi.Hal,它依赖于Web.Api 5.0及更高版本。 是否有任何重大变化?

绝对最小值重现问题

  1. 在VS Express for Web 2013中创建新的,完全空的Web API项目
  2. 添加控制器并测试它是否有效
  3. 安装CORS
  4. 文件加载错误 – 控制器不再起作用
  5. 升级Web Api – 修复问题

    安装包Microsoft.AspNet.WebApi -Pre

  6. 安装WebApi.Hal – 再次中断相同的错误

我可以使用旧版本的CORS库而不会杀死其他所有内容吗?

详细的堆栈跟踪

 === Pre-bind state information === LOG: DisplayName = System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///C: LOG: Initial PrivatePath = C Calling assembly : WebApi.Hal, Version=2.2.0.18, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: C: LOG: Using host configuration file: C:\ LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting.DLL. LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL. LOG: Attempting download of new URL file:///C:/CORSTest/CORSTest/bin/System.Net.Http.Formatting.DLL. WRN: Comparing the assembly name resulted in the mismatch: Minor Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. 

解决问题的两个步骤

  1. dependentAssembly assemblyIdentity name =“System.Web.Http”publicKeyToken =“31bf3856ad364e35”bindingRedirect oldVersion =“0.0.0.0-5.0.0.0”newVersion =“5.0.0.0”dependentAssembly

到web.config

  1. 转到工具/ Library Packed Manager / Manage Nuget Package找到解决方案,然后单击Microsoft ASP.Net MVC,然后单击Update。

希望它有所帮助

Tam Nguyen

问题是由于System.Net.Http.Formatting以及System.Web.Http程序集的版本冲突造成的。 如果在构建代码后看到输出窗口,它会为您提供问题的解决方案。 您所要做的就是将以下内容添加到Web.config文件中:

   

尝试重新安装:

 Install-Package Microsoft.AspNet.WebApi.Cors 

Microsoft.AspNet.WebApi.Cors包将依赖关系引入版本5.2.3.0 dll,如上所示的用户sumit。 我采取的路径是更新MVC项目proir以安装CORS:

 Install-Package Microsoft.AspNet.Mvc -Version 5.2.3 

解决此问题有两个步骤

  1. 更新asp.net mvc nuget包

    安装包Microsoft.AspNet.Mvc -Version 5.2.3

  2. 使用新的dependentAssembly更新web.config

    assemblyIdentity name =“System.Web.Http”publicKeyToken =“31bf3856ad364e35”bindingRedirect oldVersion =“0.0.0.0-5.0.0.0”newVersion =“5.2.3.0”