简体   繁体   中英

ASP.NET MVC 4 WebAPI PostAsJsonAsync Newtonsoft.Json error

In my MVC 4 Web API project stops working. It's can't find 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

Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I know that MS is using this as the default JSON serializer now - and it's referenced. I tried to update Newtonsoft.Json from NuGet but I can't find it; I find "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. 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

Try to add this section in your web.config file inside <runtime><assemblyBinding> :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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