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?
I got the same problem. And I solved it. I put the default constructor to the DTO class.
Ex:
Hope it work with you!
Default Entity 6 use XML to apis, in your project, find the file "Global.asax" File and add this line:
This line remove the XML Formatter.
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:
Now only JSON results will be returned. If you need XML results, you will need to find a different solution.
** 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. **
This was the specific error I was getting back from my odata Web API call:
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!!
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.
The other option is to not use the native JSON serializer and run this override in the Register method of the WebApi Config: