failed to serialize the response in Web API

2020-01-24 02:23发布

I was working on ASP.NET MVC web API, I'm having this error:

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

My controller is:

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

why I m getting this error?

15条回答
太酷不给撩
2楼-- · 2020-01-24 02:34

I got the same problem. And I solved it. I put the default constructor to the DTO class.

Ex:

public class User
{
    public User()
    {
    }
}

Hope it work with you!

查看更多
够拽才男人
3楼-- · 2020-01-24 02:35

Default Entity 6 use XML to apis, in your project, find the file "Global.asax" File and add this line:

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

This line remove the XML Formatter.

查看更多
相关推荐>>
4楼-- · 2020-01-24 02:36

For me this was a problem with circular referencing.

The accepted answer did not work for me because it only changes the behaviour of the JSON formatter, but I was getting XML when I called the service from the browser.

To fix this, I switched off XML and forced only JSON to be returned.

In the Global.asax file, put 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 a different solution.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-01-24 02:36

** this bug occur when calling from request web api/wcf/... from client side, but as side effect, you will need to include depending relations by include keyword. **

public CustomerPortalContext()
            : base("Name=CustomerPortalContext")
        {
            base.Configuration.ProxyCreationEnabled = false;
        }
查看更多
一夜七次
6楼-- · 2020-01-24 02:36

This was the specific error I was getting back from my odata Web API call:

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

I finally figured out that my dbContext class had a poorly formatted table name being assigned in onModelCreating.. so the SqlClient was dying looking for a table that didn't exist in my db!!

查看更多
Explosion°爆炸
7楼-- · 2020-01-24 02:41

I found two solutions to this. The first and easiest to implement is to change any IEnumerables, ICollections to a type of List. The WebAPI can serialize this objects, it however 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
 }

The other option is to not use the native JSON serializer and run this override in the Register method of the WebApi Config:

        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        config.Formatters.Remove(config.Formatters.XmlFormatter);
查看更多
登录 后发表回答