net::ERR_CONNECTION_RESET 200 (OK) when doing GET method

18,996

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;
});
Share:
18,996
Mahdi Jaber
Author by

Mahdi Jaber

Updated on June 04, 2022

Comments

  • Mahdi Jaber
    Mahdi Jaber almost 2 years

    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
    Mahdi Jaber about 5 years
    curl "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?
  • Derviş Kayımbaşıoğlu
    Derviş Kayımbaşıoğlu about 5 years
    of coure I don't. you need to compare this request with postman request.
  • Derviş Kayımbaşıoğlu
    Derviş Kayımbaşıoğlu about 5 years
    in here your request looks xml request. But I believe that postman tries to request json data. check this first
  • Mahdi Jaber
    Mahdi Jaber about 5 years
    How do I compare it Postman is only saying There as an error connecting to localhost:54632/api/Apartments.
  • Mahdi Jaber
    Mahdi Jaber about 5 years
    Btw on chrome it also gives the same error but the apartments are being fetched while on Postman they aren't
  • Tang Thanh Tam
    Tang Thanh Tam almost 5 years
    Thanks, My object reference to each other like this example: stackoverflow.com/a/23461179/1979190