C# 如何强制WebAPI使用JSON.net 6.0.3而不是4.5?

C# 如何强制WebAPI使用JSON.net 6.0.3而不是4.5?,c#,json.net,asp.net-web-api2,asp.net-mvc-5.1,C#,Json.net,Asp.net Web Api2,Asp.net Mvc 5.1,添加WebAPI后,将其注册到Global.asax 我们发现我们的web应用程序在以下行中断: Line 17: GlobalConfiguration.Configure(WebApiConfig.Register); 错误消息: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' o

添加
WebAPI
后,将其注册到
Global.asax

我们发现我们的web应用程序在以下行中断:

Line 17:             GlobalConfiguration.Configure(WebApiConfig.Register);
错误消息:

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, 
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. 
The system cannot find the file specified.
经过一些检查,我发现我们在这个
MVC5.1
应用程序中使用了
Json.net 6
。这是否意味着我们必须降级到
Json.net 4.5
,才能让
WebAPI
正常工作


在my
.csproj
文件中,只有一个条目:

<Reference Include="Newtonsoft.Json, Version=6.0.3.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Private>False</Private>
</Reference>
但是,如果我查看visual studio中web项目的
引用
Newtonsoft.Json
的路径指向
C:\ProgramFiles(x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll
,但
Copy Local
为false


这怎么可能?我们如何处理此冲突?

您需要在web.config中添加绑定重定向(可能与现有绑定重定向合并):


好的,这是我的解决方案

有一件事我不知道,Json.net引用首先是如何指向
Blend
文件夹中的dll的

我试着重新使用NuGet,但发现它相当不方便,因为WebApi和WebBleep都依赖于它

所以我只是继续删除了那个引用。这当然破坏了所有相关的东西

当添加回引用时,我只需通过浏览此项目中
/.package
文件夹下的dll来添加引用

它起作用了

相当残忍?确保我们查过了

  • csproj先生
  • Web.Config
  • VS中引用项中的属性

在所有基础都被覆盖之后,敢于尝试。

在我将Web Api更新到最新版本之前,重定向对我不起作用:

PM> update-package Microsoft.AspNet.WebApi.Client -Version 4.0.30506
Updating 'Microsoft.AspNet.WebApi.Client' from version '4.0.20710.0' to '4.0.30506.0' in project 'TestProject.Api'.



您确定代码是正确的,并且不应该是
WebApiConfig.Register(GlobalConfiguration.Configuration)?该寄存器语句来自。这是正确的吗?按照戴维德的建议进行更正。同样的错误。很肯定这不是解决办法。”因为这个声明已经存在了。但感谢您的输入。@Blaise之所以使用它,是因为几天前我遇到了完全相同的问题(同一行上出现了相同的异常),这就是解决方案。@Blaise您是否尝试过重新安装NuGet软件包并将
CopyLocal
设置为
true
?这在我的项目中是
true
。@Blaise这实际上对我有效,但一定要放在6.0.0.0中,而不是确切的版本。这对我有效,除了配置中的一个小调整:
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
PM> update-package Microsoft.AspNet.WebApi.Client -Version 4.0.30506
Updating 'Microsoft.AspNet.WebApi.Client' from version '4.0.20710.0' to '4.0.30506.0' in project 'TestProject.Api'.
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>