0

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:

Error message from my 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
Fred
  • 397
  • 2
  • 5
  • 17
  • 1
    If a student has many courses and a course has many students the serializer is probably getting into an infinite loop. Have a look at https://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api. – user1378730 Nov 06 '17 at 23:38
  • Method's response type expects an array of students so returning an IQueryable could be the problem, adding toArray at the end might do it. – etuncoz Nov 07 '17 at 01:35
  • https://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api – Akash KC Nov 07 '17 at 02:18

3 Answers3

0

You should cast your LINQ query to array -

Student[]

Nick Reshetinsky
  • 447
  • 2
  • 13
0

I want to GET the information about the Students and just the name of the Course they're enrolled in.

If you want just the names of the courses then select only the names of the courses

var Students = db.Students.Include("Courses").Select(st =>
                  new { 
                      Id = st.Id, 
                      FirstName = st.FirstName, 
                      LastName = st.LastName, 
                      Courses = st.Courses.Select(course => course.Name).ToArray() 
                  }).ToArray();
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

It is a Serialization problem.

As it is said in this answer you cannot serialize, by default, anonymous types to xml.

In your cliente, change the contente type to "application/json"

Pedro Drewanz
  • 1,242
  • 13
  • 14