79093530

Date: 2024-10-16 10:07:10
Score: 0.5
Natty:
Report link

To answer my own question: In Program.cs file I've added:

builder.Services.AddControllers().AddJsonOptions(options => {
    options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
});

Also, I've included Emails and Phones Attributes in the Controller:

[HttpGet("{id}")]
public async Task<ActionResult<Contact>> GetOneContact(int id)
{
    var contact = await _context.Contacts.Include(c => c.Emails).Include(c => c.Phones).FirstOrDefaultAsync(c => c.Id == id);
    if (contact == null) return NotFound();
    return contact;
}

In the end, I've added [JsonIgnore] annotation to my Contact attribute in both Phone and Email models.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Frane Krapić