相关文章推荐
长情的回锅肉  ·  How to import my dbc ...·  2 年前    · 
眉毛粗的木瓜  ·  IMS error in logs: ...·  2 年前    · 
销魂的海豚  ·  LinQ to SQL 使用 Any() ...·  2 年前    · 
旅行中的烈马  ·  css - ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;
                Thanks, My object reference to each other like this example: stackoverflow.com/a/23461179/1979190
– Tang Thanh Tam
                Jun 23, 2019 at 4:21

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.

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? – Mahdi Jaber Feb 25, 2019 at 21:36 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, 2019 at 21:44 How do I compare it Postman is only saying There as an error connecting to localhost:54632/api/Apartments. – Mahdi Jaber Feb 25, 2019 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, 2019 at 21:49

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.