安装CORS后无法加载文件

use*_*128 8 .net c# asp.net asp.net-web-api

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

一旦我安装CORS包:

Install-Package Microsoft.AspNet.WebApi.Cors -Pre
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Install-Package Microsoft.AspNet.WebApi -Pre
Run Code Online (Sandbox Code Playgroud)

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

在项目中我使用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.
Run Code Online (Sandbox Code Playgroud)

小智 2

解决问题的两步

  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. 转到工具/库打包管理器/管理 Nuget 包获取解决方案,然后单击 Microsoft ASP.Net MVC,然后单击更新。

希望有帮助

谭阮