1

While converting an object to HttpContent , compilation succeeds. But while running I am getting below error.

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.

My Code snippet:

 JArray jarray = (JArray)response;  //response is of type var.    
 IEnumerable<MyModelClass> appResponse = jarray.ToObject<IEnumerable<MyModelClass>>();    
 HttpResponseMessage msg = new HttpResponseMessage();    
 msg.Content = new ObjectContent<IEnumerable<ConfigurationResponseDomainModel>>(appResponse, new JsonMediaTypeFormatter(), "application/json");

This line throws error. Assigning object to content. I am using Newtonsoft.Json(10.0.3). Any help will be appreciated.

5
  • Clean & rebuild help? Commented Apr 18, 2018 at 6:49
  • From stan: Does your project have different version of newton ? Commented Apr 18, 2018 at 7:32
  • Add a binding redirect to web.config. Better yet, add JSON.NET as a NuGet package. It will automatically add the redirects. The latest version is 11.0.2 Commented Apr 18, 2018 at 7:33
  • BTW you could just search for that error message and find a lot of duplicate questions Commented Apr 18, 2018 at 7:34
  • BTW in ASP.NET MVC you could remove all of this code and simply use JSON.NET to serialize your actual objects to a string and return it with a StringContent. In Web API simply return your objects eg with Ok() or Json(), it uses JSON.NET already to serialize responses to JSON. .NET Core uses JSON.NET throughout Commented Apr 18, 2018 at 7:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.