【问题标题】:net::ERR_CONNECTION_RESET 200 (OK) when doing GET method执行 GET 方法时 net::ERR_CONNECTION_RESET 200 (OK)
【发布时间】:2019-07-19 08:00:56
【问题描述】:

我更改了我的公寓模型类,将作为外键的买家 ID 添加到另一个买家类,如下所示:

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 (OK)

唯一不起作用的 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 上尝试,它会返回错误。此错误的原因可能是什么? 谢谢你。

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题,这是由于在我试图序列化的数据中创建了一个自引用循环。查看您最近所做的更改,您似乎还创建了一个具有自引用循环的对象树,方法是从公寓引用回买家。

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

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • curl "localhost:54632/api/Apartments" -H "连接:保持活动状态" -H "缓存控制:max-age=0" -H "升级不安全请求:1" -H "用户代理: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" -- 压缩后的 cURL 你知道吗?
    • 我当然不知道。您需要将此请求与邮递员请求进行比较。
    • 在这里您的请求看起来是 xml 请求。但我相信邮递员会尝试请求 json 数据。先检查一下
    • 我如何比较它 Postman 只是说 There 作为连接到 localhost:54632/api/Apartments 的错误。
    • 顺便说一句,在 chrome 上它也给出了同样的错误,但是在 Postman 上却没有获取公寓
    猜你喜欢
    • 2011-03-28
    • 2021-06-27
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多