简体   繁体   中英

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

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.

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;
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM