C# net::执行GET方法时出错连接重置200(确定)
我更改了我的公寓模型类,添加了一个BuyerID,它是另一个买家类的外键,如下所示:C# net::执行GET方法时出错连接重置200(确定),c#,angular,asp.net-web-api,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 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; }
}
公共类买家
{
[关键]
公共int ID{get;set;}
公共字符串全名{get;set;}
公共积分{get;set;}
公用ICollection网络::错误\u连接\u重置200(正常)
唯一不起作用的GET方法是:
// GET: api/Apartments
[HttpGet]
public IEnumerable<Apartment> GetApartments()
{
return _context.Apartments;
}
//GET:api/公寓
[HttpGet]
公共IEnumerable公寓
{
返回式公寓;
}
否则,其他人,如:
// 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);
}
//GET:api/repartments/5
[HttpGet(“{id}”)]
公共异步任务Get公寓([FromRoute]int id)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
var单元=wait _context.plants.SingleOrDefaultAsync(m=>m.ID==ID);
if(单元==null)
{
返回NotFound();
}
返回Ok(公寓);
}
工作正常。如果我在chrome上尝试链接,它会返回公寓,但如果我在Postman或Angular应用程序上尝试,它会返回错误。这个错误的原因是什么?
谢谢。打开chrome,然后按F12打开DevTools并导航到网络选项卡。找到您的API请求并选择复制>复制为cURL
现在,您可以比较curl请求和postman请求以查看差异。差异会给您带来问题。我也有同样的问题,这是由于在我试图序列化的数据中创建了自引用循环。查看最近所做的更改,您似乎还创建了一个带有se的对象树lf通过引用返回公寓中的买家来引用循环
Json.Net对此感到不安并放弃了。我希望会像中一样抛出异常,但我没有得到异常,我的症状与您描述的相同
如果您有相同的根问题,可以通过将JSON.Net设置为在启动配置期间检测并忽略自引用循环来解决,如所述或所述
Asp.Net:
HttpConfiguration config=GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter
.序列化设置
.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Asp.net核心:
services.AddMvc().AddJsonOptions(选项=>
{
options.SerializerSettings.ReferenceLoopHandling=
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
curl”“-H”连接:保持活动状态“-H”缓存控制:最大年龄=0“-H”升级不安全请求:1“-H”用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/72.0.3626.109 Safari/537.36“-H”接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8“--H接受编码:gzip,deflate,br“-H”接受语言:en-US,en;q=0.9"--压缩这是卷曲你知道吗?当然我不知道。你需要将此请求与邮递员请求进行比较。在这里,你的请求看起来像xml请求。但我相信邮递员试图请求json数据。首先检查一下,我如何比较邮递员只是说连接到时出错。顺便说一句,在chrome上,它也给出了相同的结果错误,但在邮递员上取回公寓时,它们不是Hanks,我的对象相互引用如下示例: