I'm making a UWP application, using Entity Framework, with WebApi about Students and Courses. I have amany-to-many relationship between my Students and Courses. I tried to route an action that combines students and their courses from the database tables. I want to GET the information about the Students and just the name of the Course they're enrolled in.
Here is my error message:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Here is my custom route in my custom controller file.
StudentsCoursesController
public class StudentsCoursesController : ApiController
{
private DataContext db = new DataContext();
//GET: api/Students/Courses
[HttpGet]
[Route("api/Students/Courses")]
[ResponseType(typeof(Students[]))]
public IHttpActionResult GetstudentsCourse()
{
var Student = from st in db.Students.Include("Courses")
select new { st.Id, st.FirstName, st.LastName, st.Courses };
return Ok(Student);
}
}
This is the DataContext constructor:
DataContext:
public DataContext() : base("Database")
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
Database.SetInitializer(new LibraryDBInitializer());
}
Here is the error message I get in my Web Api:
I'm not getting any errors in my code, but I'm not getting any results either. Any ideas what could be wrong? I have a feeling the error is in my API code (the controller).
- Thanks