Я получаю это сообщение, когда пытаюсь получить доступ к API через http://localhost:XXXXX/api/Employees. Я использовал следующий код для получения данных из базы данных, но API дал мне следующую ошибку
сообщение об ошибке
> <Error> <Message>An error has occurred.</Message> <ExceptionMessage>
> The 'ObjectContent`1' type failed to serialize the response body for
> content type 'application/xml; charset=utf-8'. </ExceptionMessage>
> <ExceptionType>System.InvalidOperationException</ExceptionType>
> <StackTrace/> <InnerException> <Message>An error has
> occurred.</Message> <ExceptionMessage> Type
>
>f__AnonymousType5`4 [System.String,System.String,System.String,System.String]'
> cannot be serialized. Consider marking it with the
> DataContractAttribute attribute, and marking all of its members you
> want serialized with the DataMemberAttribute attribute. If the type is
> a collection, consider marking it with the
> CollectionDataContractAttribute. See the Microsoft .NET Framework
> documentation for other supported types. </ExceptionMessage>
> <ExceptionType>
> System.Runtime.Serialization.InvalidDataContractException
> </ExceptionType>
вот код, который я использовал
public IHttpActionResult GetEmployees()
{
var query = (from n in db.Employees
join c in db.tblCities on n.ProjectID equals c.CityID
into nc
from c in nc.DefaultIfEmpty()
join parent in db.Employees on n.ManagerName equals parent.Name
into pc
from parent in pc.DefaultIfEmpty()
select new
{
n.Name,
ManagerName = parent.Name,
n.Email,
c.CityName
});
var employees = query.ToList();
//some other things, maybe you want to return BadRequest if there are none or NotFound or whatever you deem appropriate
return Ok(employees);
}