I am working on an ASP.NET Web API simply trying to return some data from my database, but I keep getting this error:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
I have tried adding the following to the Global.asax file as suggested in other questions similar to mine, but all it does is change the formatting of the error.
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
My controller looks like the following:
public class SQLController : ApiController
{
public IEnumerable<Alias> Get()
{
using (DatabaseEntities entities = new DatabaseEntities())
{
return entities.Aliases.ToList();
}
}
public Alias Get(int id)
{
using (DatabaseEntities entities = new DatabaseEntities())
{
return entities.Aliases.FirstOrDefault(a => a.ID == id);
}
}
}
I tried something very similar with a much simpler database and it worked fine. I have also read that making a separate data model could fix this issue? if so why, and how would I go about doing that? Any help with figuring out why I am receiving this error would be appreciated.