GCoe GCoe - 4 months ago 27
AngularJS Question

Web API error 500 from method

I have an Angularjs front end and a Web API backend.

I'm getting an error 500 from a method when returned object contains a list.No problem when the list is not present :

If I return an object like this there is no error :

var myObj = new { name1 = value1, name2 = value2};
return myObj;


But for the following I got and error 500 :

var q = from p in db.SomeThing select p;
var MyObj = new { name1 = value1, name2 = value2, name3 = q.ToList()};


Some error 500 for :

var q = from p in db.SomeThing select p;
var MyObj = new { name1 = value1, name2 = value2, name3 = q};


What could be wrong ?

Answer

The entities returned from db.SomeThing my have navigation properties that are causing the exception when they are executed during serialization. Try selecting an anonymous object instead of p and see if that solves your problem.

var q = from p in db.SomeThing 
        select new { 
            property1 = p.property1, 
            property2 = p.property2,
            //other properties
        };