简体   繁体   English

ASP.NET MVC 4 WebAPI PostAsJsonAsync Newtonsoft.Json错误

[英]ASP.NET MVC 4 WebAPI PostAsJsonAsync Newtonsoft.Json error

In my MVC 4 Web API project stops working. 在我的MVC 4 Web API项目中停止工作。 It's can't find Newtonsoft.Json. 找不到Newtonsoft.Json。 After running this code : 运行此代码后:

 Dim response As HttpResponseMessage = MyHttpClient.PostAsJsonAsync("Api/Test", MyObject).Result

I get message error : 我收到消息错误:

An unhandled exception of type 'System.IO.FileLoadException' occurred in System.Net.Http.Formatting.dll System.Net.Http.Formatting.dll中发生了类型为'System.IO.FileLoadException'的未处理异常

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. 附加信息:无法加载文件或程序集“ Newtonsoft.Json,版本= 4.5.0.0,区域性=中性,PublicKeyToken = 30ad4fe6b2a6aeed”或其依赖项之一。 The located assembly's manifest definition does not match the assembly reference. 找到的程序集的清单定义与程序集引用不匹配。 (Exception from HRESULT: 0x80131040) (来自HRESULT的异常:0x80131040)

I know that MS is using this as the default JSON serializer now - and it's referenced. 我知道MS现在将其用作默认的JSON序列化程序-并且已被引用。 I tried to update Newtonsoft.Json from NuGet but I can't find it; 我尝试从NuGet更新Newtonsoft.Json,但找不到它; I find "Json.Net". 我找到“ Json.Net”。 So I used package manager console to reinstall 所以我用包管理器控制台重新安装

Update-Package Newtonsoft.Json –Reinstall

But still doesn't work. 但是仍然不起作用。 Does anyone have any idea why is this going wrong? 有人知道为什么会出错吗?

It seems that you are using some outdated library that depends on an old version of JSON.NET. 似乎您正在使用一些过时的库,该库依赖于旧版本的JSON.NET。 You could try to install the specific version of it: 您可以尝试安装它的特定版本:

Update-Package Newtonsoft.Json -Version 4.5.11

If this fails telling you that some other package requires a newer version of JSON.NET then you will need to resolve this conflict by upgrading the outdated library that you are using and which depends on JSON.NET 4.5 如果此操作告诉您其他软件包需要较新版本的JSON.NET,那么您将需要通过升级正在使用且取决于JSON.NET 4.5的过时的库来解决此冲突。

Try to add this section in your web.config file inside <runtime><assemblyBinding> : 尝试在<runtime><assemblyBinding>中的web.config文件中添加此部分:

<dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM