net :: ERR_CONNECTION_RESET 200(确定),当执行GET方法时

时间:2019-02-25 20:37:42

标签: c# angular asp.net-web-api

我通过向另一个买方类添加外键BuyerID来更改了我的公寓模型类:

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

    }

我的买方模型类别如下:

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

因此它也包含公寓的集合。 并且由于这个原因,我的Get方法可能不再起作用,并返回以下错误:GET http://localhost:54632/api/Apartments net :: ERR_CONNECTION_RESET 200(确定)

唯一无法使用的GET方法是以下方法:

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

否则,其他诸如此类的

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

正常工作。如果我尝试使用chrome上的链接,它会返回公寓,但是如果我在Postman或Angular App上尝试它,则会返回错误。造成此错误的原因是什么? 谢谢。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,这是由于在我要序列化的数据中创建了一个自引用循环。查看最近所做的更改,就好像您还通过从Apartments引用买家来创建带有自引用循环的对象树一样。

Json.Net对此感到沮丧并放弃了。我希望会像在this question中一样引发异常,但是我没有得到一个异常,我的症状与您描述的相同。

如果您遇到相同的根本问题,可以通过设置JSON.Net来解决,如herehere for asp.net core所述,在启动配置过程中检测并忽略自引用循环。

Asp.Net:

HttpConfiguration config = GlobalConfiguration.Configuration;

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

Asp.net核心:

services.AddMvc().AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = 
                               Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

答案 1 :(得分:0)

打开chrome,然后按F12打开DevTools并导航到“网络”选项卡。找到您的API请求,然后选择复制>复制为cURL

现在,您可以比较curl请求和邮递员请求以查看差异。差异会给您带来问题。

相关问题