118

I am working with ASP.NET MVC 5 Web Api. I want consult all my users.

I wrote api/users and I receive this:

"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'"

In WebApiConfig, already I added these lines:

HttpConfiguration config = new HttpConfiguration();
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 

But it still doesn't work.

My function for return data is this:

public IEnumerable<User> GetAll()
{
    using (Database db = new Database())
    {
        return db.Users.ToList();
    }
}
chakeda
  • 1,551
  • 1
  • 18
  • 40
CampDev
  • 1,659
  • 3
  • 16
  • 22
  • What does the value object look like that you are trying to pass to the consumer? – gudatcomputers Apr 16 '14 at 03:16
  • Thanks so much! Just fyi - I think that should read: using (Database db = new Database ()) { List listOfUsers = new List(); foreach(var user in db.Users) { UserModel userModel = new UserModel(); userModel.FirstName = user.FirstName; userModel.LastName = user.LastName; listOfUsers.Add(userModel); } IEnumerable users = listOfUsers; return users; } ..as the results were all returning the same values. – Jared Whittington Jul 09 '15 at 02:28
  • 1
    Possible duplicate of [failed to serialize the response in Web API](http://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api) – Edvaldo Silva Feb 20 '17 at 19:45
  • Ahh after hours of debugging in my case i forgot to add migrations for model change. – sairfan Mar 13 '23 at 17:13

27 Answers27

155

If you are working 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);          

Dont`t forget to import

using System.Data.Entity;

Then you can return your own EF Models

Simple as that!

Markus
  • 20,838
  • 4
  • 31
  • 55
Lucas Roselli
  • 2,810
  • 1
  • 15
  • 18
  • Even if it might help for EF, the solution is not specific to EF, and work also with other kind of models. The using doesn't seem to be necessary in Global.asax. Was it intended for the controllers? – Matthieu Mar 31 '16 at 15:18
  • 18
    Some explanations about what this code does and its implications would be welcomed. – Jacob Mar 27 '17 at 18:12
  • 1
    Thank I was facing the similar problem, this answer helped me to solve the issue. – RK_Aus Oct 13 '17 at 00:33
  • It works for me. No need to add using System.Data.Entity; to global.asax. Thank you. – Dr. MAF Jan 01 '19 at 16:27
  • It works. Simple add code above on Global.asax, thats all, No need to import using System.Data.Entity; – Hemant Ramphul Oct 01 '19 at 19:15
83

When it comes to returning data back to the consumer from Web Api (or any other web service for that matter), I highly recommend not passing back entities that come from a database. It is much more reliable and maintainable to use Models in which you have control of what the data looks like and not the database. That way you don't have to mess around with the formatters so much in the WebApiConfig. You can just create a UserModel that has child Models as properties and get rid of the reference loops in the return objects. That makes the serializer much happier.

Also, it isn't necessary to remove formatters or supported media types typically if you are just specifying the "Accepts" header in the request. Playing around with that stuff can sometimes make things more confusing.

Example:

public class UserModel {
    public string Name {get;set;}
    public string Age {get;set;}
    // Other properties here that do not reference another UserModel class.
}
jensendp
  • 2,135
  • 15
  • 15
  • When you refer to Models, you want say what I am doing? Return IEnumerable of Users who is a Model. – CampDev Apr 16 '14 at 03:31
  • 5
    You are returning an Entity. An entity refers to an object in the DB that can be retrieved by a unique id. You are returning all the User entities from your database. I'm suggesting that you create a new class called "UserModel" and for each of the User entities you get from the database, create a new instance of the data model class populated with the necessary info you want to expose. The return an IEnumerable of UserModel objects as opposed to User entities. Make sure the model Properties don't refer to instances of the UserModel class. That is what is getting you into this problem. – jensendp Apr 16 '14 at 03:41
  • 3
    ncampuzano is correct, you don't want to return auto generated entities. If you were using stored procedures for database access the separation would be more clear. You would need to have generated a C# value object and mapped values from the IDataReader to the value object. Since you're using EF, there are classes being generated for you, but those are special EF classes that know more then value objects do. You should only return "dumb" value objects to your client. – gudatcomputers Apr 16 '14 at 04:14
  • @jensendp using your suggested method of implementing the model objects, at which point do you map the entity to the model. Is this done in your Api controller or is it done in a separate layer? – Donald N. Mafa Sep 21 '15 at 16:57
  • 1
    @Donny If you're using DBContext or a Repository in your controller that is returning entities back from the DB, then you can just map the objects to models (a DTO for example) in the controller... but I prefer to have the controller call a service that returns the model/DTO. Check out AutoMapper - great tool for handling mapping. – ben Nov 07 '15 at 22:56
  • What a pain, though! What is the point of the entire LINQ-to-SQL schenangains if we can't even **use** the classes it auto-generates?!?! – NH. Oct 30 '17 at 19:59
  • 1
    @NH. You can absolutely use the aforementioned shenanigans, but everything has it's place. The "entities" provided by access to the Data Layer should typically stay in the Data Layer. Anything that wants to use this data within the Business Layer of the application will typically use the "entities" in a transformed form as well (Domain Objects). And then the data that is returned to and input from the user will typically be another form as well (Models). Agreed it can be tedious to do this type of transformation everywhere, but that is where tools like AutoMapper really come in handy. – jensendp Nov 06 '17 at 20:33
54

Given right answer is one way to go, however it is an overkill when you can fix it by one config settings.

Better to use it in the dbcontext constructor

public DbContext() // dbcontext constructor
            : base("name=ConnectionStringNameFromWebConfig")
{
     this.Configuration.LazyLoadingEnabled = false;
     this.Configuration.ProxyCreationEnabled = false;
}

Asp.Net Web API Error: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'

Community
  • 1
  • 1
Md. Alim Ul Karim
  • 2,401
  • 2
  • 27
  • 36
38

Add this code to global.asax below on Application_Start:

Update from .Ignore to .Serialize . It must work.

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
            GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
Bimal Das
  • 1,882
  • 5
  • 28
  • 53
  • 1
    Works great thanks!, Could you explain better why do we have to remove the Xml Formatter to make it work? – Jav_1 Oct 26 '17 at 16:38
  • 1
    There is no need to add the json serialiser (at least in my case) but removing the Xml formatting was needed. I *guess* the xml serialiser can't serialise anonymous types and by removing it the result is serialised as json. If my guess is correct one would be able to get data out of the controller by asking for MIME type "application/json". – LosManos Jan 22 '20 at 19:25
11
public class UserController : ApiController
{

   Database db = new Database();

   // construction
   public UserController()
   {
      // Add the following code
      // problem will be solved
      db.Configuration.ProxyCreationEnabled = false;
   }

   public IEnumerable<User> GetAll()
    {
            return db.Users.ToList();
    }
}
Aykut
  • 119
  • 1
  • 3
11

I resolved it using this code to WebApiConfig.cs file

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; 
config.Formatters.Remove(config.Formatters.XmlFormatter);
aemre
  • 2,351
  • 2
  • 17
  • 20
10

I don't like this code:

foreach(var user in db.Users)

As an alternative, one might do something like this, which worked for me:

var listOfUsers = db.Users.Select(r => new UserModel
                         {
                             userModel.FirstName = r.FirstName;
                             userModel.LastName = r.LastName;

                         });

return listOfUsers.ToList();

However, I ended up using Lucas Roselli's solution.

Update: Simplified by returning an anonymous object:

var listOfUsers = db.Users.Select(r => new 
                         {
                             FirstName = r.FirstName;
                             LastName = r.LastName;
                         });

return listOfUsers.ToList();
Donut
  • 110,061
  • 20
  • 134
  • 146
Kasper Jensen
  • 548
  • 5
  • 12
7

Adding this in your Application_Start() method of Global.asax file should solve the problem

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
        .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    GlobalConfiguration.Configuration.Formatters
        .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter); 
// ...
}

METHOD 2: [Not recommended]
If you are working with EntityFramework, you can disable proxy in your DbContext class constructor. NOTE: this code wll be removed if you update the model

public class MyDbContext : DbContext
{
  public MyDbContext()
  {
    this.Configuration.ProxyCreationEnabled = false;
  }
}
Er Suman G
  • 581
  • 5
  • 12
  • 1
    Thanks Suman. I was having the same issue. I was testing my web API & stuck with this issue. your solution resolves the issue. thanks a lot. – TarakPrajapati Jan 26 '19 at 08:22
6

There's also this scenario that generate same error:

In case of the return being a List<dynamic> to web api method

Example:

public HttpResponseMessage Get()
{
    var item = new List<dynamic> { new TestClass { Name = "Ale", Age = 30 } };

    return Request.CreateResponse(HttpStatusCode.OK, item);
}

public class TestClass
{
    public string Name { get; set; }
    public int Age { get; set; }
}

So, for this scenario use the [KnownTypeAttribute] in the return class (all of them) like this:

[KnownTypeAttribute(typeof(TestClass))]
public class TestClass
{
    public string Name { get; set; }
    public int Age { get; set; }
}

This works for me!

Alex
  • 158
  • 2
  • 7
4

My personal favorite: Just add the code below to App_Start/WebApiConfig.cs. This will return json instead of XML by default and also prevent the error you had. No need to edit Global.asax to remove XmlFormatter etc.

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
Ogglas
  • 62,132
  • 37
  • 328
  • 418
4

Just put following lines in global.asax:

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

Import

using System.Data.Entity;
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Sayyam abbasi
  • 109
  • 1
  • 8
2

Use AutoMapper...

public IEnumerable<User> GetAll()
    {
        using (Database db = new Database())
        {
            var users = AutoMapper.Mapper.DynamicMap<List<User>>(db.Users);
            return users;
        }
    }
Proximo
  • 6,235
  • 11
  • 49
  • 67
2

Use the following namespace:

using System.Web.OData;

Instead of :

using System.Web.Http.OData;

It worked for me

Mayuso
  • 1,291
  • 4
  • 19
  • 41
Harish K
  • 21
  • 1
2

Solution that worked for me:

  1. Use [DataContract] for class and [DataMember] attributes for each property to serialize. This is enough to get Json result (for ex. from fiddler).

  2. To get xml serialization write in Global.asax this code:

    var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; xml.UseXmlSerializer = true;

  3. Read this article, it helped me to understand serialization: https://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
Community
  • 1
  • 1
Lapenkov Vladimir
  • 3,066
  • 5
  • 26
  • 37
2

Add the below line

this.Configuration.ProxyCreationEnabled = false;

Two way to use ProxyCreationEnabled as false.

  1. Add it inside of DBContext Constructor

    public ProductEntities() : base("name=ProductEntities")
    {
        this.Configuration.ProxyCreationEnabled = false;
    }
    

OR

  1. Add the line inside of Get method

    public IEnumerable<Brand_Details> Get()
    {
        using (ProductEntities obj = new ProductEntities())
        {
            this.Configuration.ProxyCreationEnabled = false;
            return obj.Brand_Details.ToList();
        }
    }
    
barbsan
  • 3,418
  • 11
  • 21
  • 28
MR.S
  • 21
  • 2
2

Use [Serializable] for class:

Example:

[Serializable]
public class UserModel {
    public string Name {get;set;}
    public string Age {get;set;}
}

It worked for me!

Msxmania
  • 53
  • 2
  • 9
  • 1
    Phrased like this, one could almost think that every one who answered this post before was dumb ;)… Well, I seriously doubt they were, so it probably means that this solution was simply not applicable when the question was asked, back in 2015… I don't know much about that syntax myself, but I have the feeling that either it is relatively new, or there might be some drawbacks to it that makes it unusable in certain use cases. I have the feeling that your solution could be useful to future readers reaching this question, but it would certainly help if you'd clarify its limitations. – James Jul 22 '19 at 17:30
1

To add to jensendp's answer:

I would pass the entity to a user created model and use the values from that entity to set the values in your newly created model. For example:

public class UserInformation {
   public string Name { get; set; }
   public int Age { get; set; }

   public UserInformation(UserEntity user) {
      this.Name = user.name;
      this.Age = user.age;
   }
}

Then change your return type to: IEnumerable<UserInformation>

Greg A
  • 531
  • 4
  • 9
  • 1
    there are more elegant ways to handle the translation for you so that you don't have to maintain every property .. AutoMapper and ValueInjecter are 2 notable ones – Sonic Soul Jun 23 '15 at 15:53
1

While all these answers above are correct, one may want to check the InnerException > ExceptionMessage.

If it says something like this "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.". This could be an issue because of default behavior of the EF.

By assigning LazyLoadingEnabled = false in your DbContext constructor will do the trick.

public class MyDbContext : DbContext
{
  public MyDbContext()
  {
    this.Configuration.LazyLoadingEnabled = false;
  }
}

For more detailed reading about EagerLoading and LazyLoading behavior of EF refer this MSDN Article.

B Bhatnagar
  • 1,706
  • 4
  • 22
  • 35
1

In my case I have had similar error message:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

But when I dig deeper in it, the issue was:

Type 'name.SomeSubRootType' with data contract name 'SomeSubRootType://schemas.datacontract.org/2004/07/WhatEverService' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

The way I solved by adding KnownType.

[KnownType(typeof(SomeSubRootType))]
public partial class SomeRootStructureType

This was solved inspired from this answer.

Reference: https://msdn.microsoft.com/en-us/library/ms730167(v=vs.100).aspx

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
1

This is my error

I basically add one line which they are

  • entities.Configuration.ProxyCreationEnabled = false;

to UsersController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using UserDataAccess;

namespace SBPMS.Controllers
{
    public class UsersController : ApiController
    {


        public IEnumerable<User> Get() {
            using (SBPMSystemEntities entities = new SBPMSystemEntities()) {
                entities.Configuration.ProxyCreationEnabled = false;
                return entities.Users.ToList();
            }
        }
        public User Get(int id) {
            using (SBPMSystemEntities entities = new SBPMSystemEntities()) {
                entities.Configuration.ProxyCreationEnabled = false;
                return entities.Users.FirstOrDefault(e => e.user_ID == id);
            }
        }
    }
}

Here is my output:

1

You will have to define Serializer Formatter within WebApiConfig.cs available in App_Start Folder like

Adding config.Formatters.Remove(config.Formatters.XmlFormatter); // which will provide you data in JSON Format

Adding config.Formatters.Remove(config.Formatters.JsonFormatter); // which will provide you data in XML Format

Yogesh Dangre
  • 181
  • 1
  • 4
0

Another case where I received this error was when my database query returned a null value but my user/view model type was set as non-nullable. For example, changing my UserModel field from int to int? resolved.

0

This also happens when the Response-Type is not public! I returned an internal class as I used Visual Studio to generate me the type.

internal class --> public class
Vanice
  • 676
  • 5
  • 15
0

Visual Studio 2017 or 2019 is totally unthoughtful on this, because Visual Studio itself requires the output to be in json format, while Visual Studio's default format is "XmlFormat" (config.Formatters.XmlFormatter).

Visual Studio should do this automatically instead of giving developers so much trouble.

To correct this problem, go to the WebApiConfig.cs file, and add

var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter);

after "config.MapHttpAttributeRoutes();" in the Register(HttpConfiguration config) method. This would allow your project to produce json output.

William Hou
  • 1,251
  • 14
  • 17
0

In my case I solved recreating the database. I made some changes in a model and launching Update-Database in Package Manager Console I got the following Error:

"The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Activities_dbo.Projects_ProjectId". The conflict occurred in database "TrackEmAllContext-20190530144302", table "dbo.Projects", column 'Id'."

Manuel Sansone
  • 339
  • 4
  • 5
0

In case: If adding code to WebApiConfig.cs or Global.asax.cs doesn't work for you:

.ToList();

Add .ToList() function.

I tried out every solution but following worked for me:

var allShops = context.shops.Where(s => s.city_id == id)**.ToList()**;
return allShops;

I hope, it helps.

Catalyst
  • 465
  • 6
  • 14
0

in my case, it was fixed when I removed the virtual keyword before my navigation properties, I mean the reference tables. so I changed

public virtual MembershipType MembershipType { get; set; }

to:

public MembershipType MembershipType { get; set; }
Hadi R.
  • 403
  • 3
  • 12