2

I changed my Apartments Model Class by adding a BuyerID which is a foreign key to another Buyer Class like this:

public class Apartment
    {
        [Key]
        public int ID { get; set; }
        public string Title { get; set; }
        public int NbofRooms { get; set; }
        public int Price { get; set; }
        public string Address { get; set; }
        public int BuyerId { get; set; } 

    }

Also I have my Buyers Model Class as the following:

public class Buyer
    {
        [Key]
        public int ID { get; set; }
        public string FullName { get; set; }
        public int Credit { get; set; }
        public ICollection<Apartment> apartments { get; set; }
    }

So it also contains a collection of Apartments. and because of this maybe my Get method isn't working anymore and is returning the following error: GET http://localhost:54632/api/Apartments net::ERR_CONNECTION_RESET 200 (OK)

The only GET Method not working is this one:

// GET: api/Apartments
        [HttpGet]
        public IEnumerable<Apartment> GetApartments()
        {
            return _context.Apartments;
        }

Otherwise the others such as this:

// GET: api/Apartments/5
        [HttpGet("{id}")]
        public async Task<IActionResult> GetApartment([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var apartment = await _context.Apartments.SingleOrDefaultAsync(m => m.ID == id);

            if (apartment == null)
            {
                return NotFound();
            }

            return Ok(apartment);
        }

is working fine.Also if I try the link on chrome it returns the apartments but if I try it on Postman or Angular App it returns the error. What could be the cause of this error? Thank you.

Mahdi Jaber
  • 231
  • 1
  • 4
  • 12

2 Answers2

2

I had the same problem, and it was due to having created a self-referencing loop in the data I was trying to serialize. Looking at the recent change you had made it looks like you also created an object tree with a self referencing loop by referencing back to a Buyer from Apartments.

Json.Net gets upset by this and gives up. I would expect an exception to be thrown as in this question, but I didn't get one, I had the same symptoms as you describe.

If you are having the same root problem, it is solved by setting JSON.Net to detect and ignore self referencing loops during startup configuration as explained here or here for asp.net core.

Asp.Net:

HttpConfiguration config = GlobalConfiguration.Configuration;

config.Formatters.JsonFormatter
            .SerializerSettings
            .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

Asp.net Core:

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = 
                               Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
Wilco
  • 974
  • 8
  • 14
0

open chrome then open DevTools by pressing F12 and navigate to network tab. Find your API request and select copy > copy as cURL

now you can compare curl request and postman request in order to see difference. The difference will give you the problem.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • curl "http://localhost:54632/api/Apartments" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9" --compressed here is the cURL do you have any idea? – Mahdi Jaber Feb 25 '19 at 21:36
  • of coure I don't. you need to compare this request with postman request. – Derviş Kayımbaşıoğlu Feb 25 '19 at 21:42
  • in here your request looks xml request. But I believe that postman tries to request json data. check this first – Derviş Kayımbaşıoğlu Feb 25 '19 at 21:44
  • How do I compare it Postman is only saying There as an error connecting to http://localhost:54632/api/Apartments. – Mahdi Jaber Feb 25 '19 at 21:45
  • Btw on chrome it also gives the same error but the apartments are being fetched while on Postman they aren't – Mahdi Jaber Feb 25 '19 at 21:49