Failed to serialize response in Web API

I was working on ASP.NET MVC web API, I have this error:

The type "ObjectContent`1" could not serialize the response body for the content type "application / xml"; encoding = UTF-8 '.

My controller:

public Employee GetEmployees() { Employee employees = db.Employees.First(); return employees; } 

why am i getting this error?

+81
c # serialization asp.net-web-api
Sep 28 '12 at 13:53 on
source share
15 answers

For me it was a circular link issue.

The accepted answer did not work for me, because it changes the JSON formatting behavior, but I received XML when I called the service from the browser.

To fix this, I turned off XML and forcibly returned only JSON.

In the Global.asax file, place the following lines at the top of your Application_Start method:

 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 

Now only JSON results will be returned. If you need XML results, you will need to find another solution.

+114
Nov 27 '13 at 20:02
source share

in your global.asax file, in the Application_start () method add this line:

 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 

I hope this helps!

+39
Oct 09 :
source share

I have the same problem. And I decided that. I put the default constructor in the DTO class.

Example:

 public class User { public User() { } } 

Hope it works for you!

+29
Mar 28 '13 at 17:15
source share

Put this in the constructor. Hope this solves the problem:

  public MyController() { db.Configuration.ProxyCreationEnabled = false; } 
+20
Feb 27 '13 at 16:15
source share

I found two solutions. The first and easiest to implement is to modify any IEnumerables, ICollections for the List type. The WebAPI can serialize these objects, but it cannot serialize interface types.

 public class Store { [StringLength(5)] public string Zip5 { get; set; } public virtual List<StoreReport> StoreReports { get; set; } //use a list here } 

Another option is to not use your own JSON serializer and run this override in the Register WebApi Config method:

  var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); 
+16
Mar 06 '13 at 2:36
source share

The solution is simple.

After the LINQ query, add .ToList () (or ToDictionary, if necessary).

It will do the loading than lazy data loading.

+7
Sep 13 '13 at 17:50
source share

** This error occurs when calling from the api / wcf / ... website from the client side, but as a side effect you need to include dependent relationships for the include keyword. **

 public CustomerPortalContext() : base("Name=CustomerPortalContext") { base.Configuration.ProxyCreationEnabled = false; } 
+4
Apr 08 '14 at 16:38 on
source share

If you work with EF, besides adding the code below on Global.asax

  GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 

Do not forget to import

 using System.Data.Entity; 

Then you can return your own EF models

+4
Mar 01 '15 at 1:00
source share

please check the documentation on the api web page for this problem, Handling links to circular objects

Hello

+3
May 15 '13 at 14:14
source share

If you use web api with Entity Framework, the solution may be Failed to serialize the response in the web API using Json

Basically, you need to create a model that matches each EF model, which removes dependencies between classes and makes it easy to serialize.

Code: (taken from link)

Create UserModel

 public class UserModel { public string FirstName { get; set; } public string LastName { get; set; } } 

Change my GetAll () method

 public IEnumerable<UserModel> GetAll() { using (Database db = new Database ()) { List<UserModel> listOfUsers = new List<UserModel>(); UserModel userModel = new UserModel(); foreach(var user in db.Users) { userModel.FirstName = user.FirstName; userModel.LastName = user.LastName; listOfUsers.Add(userModel); } IEnumerable<UserModel> users = listOfUsers; return users; } } 
+3
Jan 07 '15 at 0:11
source share

hmmm, After that, it can help.

I got the same exception, and in my case, I first passed the actual poco object created for the entity code. Since it contains relationships with other objects, I just created a viewmapper / dto object on top of it to return.

Now it works great.

Poco Entity:

 public class Tag { public int Id{get;set;} public string Title{get;set;} public IList<Location> Locations{get;set;} } 

ViewMapper / Dto

 public class TagResultsViewMapper { public int Id{get;set;} public string Title{get;set;} //just remove the following relationship //public IList<Location> Locations{get;set;} } 
+1
Feb 20 '13 at 6:37
source share

but if you find this problem with other objects / classes, you need to create a new DTO for each class, and if you have a lot of them, you can find the problem, also I think that creating DTO only to solve this problem is not the best way. ..

Have you tried this?

 var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All; 

Hello

+1
Aug 14 '13 at 16:53 on
source share

The default object 6 uses XML for apis in your project, find the file "Global.asax" File and add this line:

 GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 

This line removes the XML Formatter.

+1
Feb 12 '16 at 20:19
source share

Your question is very similar to mine. You should not return data from the database directly. To do this, you need to create a model and associate the data that you want to show.

In my example, there is user data that Json cannot serialize, I created userModel, and in my API I return UserModel instead of User from the database.

The logic for transforming or associating data between User and UserModel must be in the API.

Failed to serialize response in web API using Json

0
Apr 16 '14 at 22:43
source share

This was a special error that I was returning from my Odata API call:

 The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'. 

It finally turned out that my dbContext class was assigned a poorly formatted table name in onModelCreating .. so SqlClient was dying looking for a table that did not exist in my db !!

0
Mar 09 '17 at 20:43 on
source share



All Articles