[英]net::ERR_CONNECTION_RESET 200 (OK) when doing GET method
我通過向另一個買方類添加外鍵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上嘗試,它將返回錯誤。 造成此錯誤的原因是什么? 謝謝。
我遇到了同樣的問題,這是由於在我要序列化的數據中創建了一個自引用循環。 查看最近所做的更改,就好像您還通過從Apartments引用回購者來創建帶有自引用循環的對象樹一樣。
Json.Net對此感到沮喪並放棄了。 我希望會像在此問題中一樣引發異常,但是我沒有得到一個異常,我有與您描述的症狀相同的症狀。
如果您遇到相同的根本問題,可以通過將JSON.Net設置為在啟動配置過程中檢測和忽略自引用循環來解決,如此處或此處對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;
});
打開chrome,然后按F12打開DevTools並導航至“網絡”標簽。 找到您的API請求,然后選擇復制>復制為cURL
現在,您可以比較curl請求和郵遞員請求以查看差異。 差異會給您帶來問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.