0

This is my .net core 2 web api method,

    [HttpGet]
    public List<Clients> GetClientsWithUsers() // public IEnumerable<Clients> GetClientsWithUsers()
    {
        var clientsWithUsers = _context.Clients.Include(c => c.Users).ToList(); //.ToArray()
        return clientsWithUsers;
    }

    public partial class Clients
    {
        public Clients()
        {
            Users = new HashSet<Users>();
        }

        public int ClientId { get; set; }
        public string Name { get; set; }
        public int? MaxUsers { get; set; }
        public ICollection<Users> Users { get; set; }
    }

    public partial class Users
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ClientId { get; set; }
        public DateTime LastAccessed { get; set; }
        public Clients Client { get; set; }
    }       

and the result I am getting in the browser is truncated:

[{"clientId":1,"name":"TestClientName","maxUsers":12,"Users":[{"id":3,"name":"some name","clientId":1,"lastAccessed":"2015-11-27T00:00:00"

I am not sure why I am getting broken structure.

dbc
  • 104,963
  • 20
  • 228
  • 340
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • What structure is broken? It looks fine to me. Write your expected output too. – Adrian Dec 13 '17 at 08:37
  • 1
    @Adriani6 - it has been truncated. Upload to https://jsonlint.com/ to see the problem. – dbc Dec 13 '17 at 08:38
  • @dbc Ah. Of course. Didn't see the ending of the string. I blame the phone... ignore my first comment. – Adrian Dec 13 '17 at 08:39
  • 2
    Could your framework have swallowed a [self referencing loop](https://stackoverflow.com/q/7397207/3744182) error trying to serialize `Clients.Users[0].Client`? If that happened the JSON might have been only partially written; indeed it appears to have been cut off right before serializing `Users[0].Client`. Try marking `public Clients Client` in `User` with `[JsonIgnore]`, `[IgnoreDataMember]` or `[ScriptIgnore]` as appropriate to see if the problem goes away. – dbc Dec 13 '17 at 08:44
  • @dbc you are right, I was just trying to convert it into JSON on web api side and get this error, 'Self referencing loop detected for property 'Client' with type... – Mathematics Dec 13 '17 at 08:46
  • 1
    believe this should fix the issue, going to try it, thanks, https://stackoverflow.com/questions/34892509/controller-json-set-serialization-referenceloophandling – Mathematics Dec 13 '17 at 08:50
  • Nope, it's not fixing the issue for me, there seem to be a issue on git but no solution, https://github.com/aspnet/Home/issues/2285 – Mathematics Dec 13 '17 at 09:26
  • What if you just add `[IgnoreDataMember]` and/or `[JsonIgnore]` to `Users.Client`? Do you want to serialize it, or skip it? – dbc Dec 13 '17 at 09:46
  • for some queries I will need it too, but I guess if I am not removing it completely there still be some way... – Mathematics Dec 13 '17 at 12:58

0 Answers0