【发布时间】: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