【发布时间】:2021-04-08 22:19:59
【问题描述】:
我已经制作了一个简单的仓库管理系统,并且该站点本身运行良好 - 但是尝试使用 Postman POST 进行 API 测试并不能特别适用于尝试发布具有两个外键指向的货物时customerID 和货件 ID。
我的数据库目前由 Customer、Shipment 和 Cargo 表组成。
我可以通过 Postman 发布一个实际的 Shipment 条目,因为它没有外键。但是,当我尝试发布具有 Shipment & Customer 外键的货物时,邮递员一直告诉我“找不到 405 方法”
我也为此特定目的创建了 -API 控制器。货物运行良好。
为了测试目的,我尝试构建 APICargoSend 控制器中的两种方法。
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult<Cargo>> PostCargo(Cargo cargo)
{
_context.Cargo.Add (cargo);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCargo", new { id = cargo.Id }, cargo);
}
[HttpPost] // post method
[AllowAnonymous]
public async Task<IActionResult>
CargoPost(
[
Bind(
"Id,TypeOfPallet,Length,Width,Height,Weight,Damage,Shipment,Customer")
]
Cargo cargo
)
{
if (ModelState.IsValid)
{
_context.Cargo.Add (cargo);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCargo",
new { id = cargo.Id },
cargo);
}
return Ok("Succeeded");
}
这是我的货物数据库模型
{public int Id { get; set; }
[DisplayName("Type of Pallet")]
public PalletType TypeOfPallet { get; set; }
public double Length { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Weight { get; set; }
public bool Damage { get; set; }
public Shipment Shipment { get; set; }
public Customer Customer { get; set; }
}
public enum PalletType
{
HeatTreated,
Wooden,
Metal,
Tote,
Crate,
SkeletonCrate,
NonHeatTreated,
EuropeanPallet
}
这是我的 POSTMAN 代码 - 我尝试了各种不同的方法都无济于事。
"Id": 20,
"TypeOfPallet": 0,
"Length": 20,
"Width": 40,
"Height": 50,
"Weight": 120,
"Damage": true,
"foreign_key": {
"Shipment": 1,
"Customer": 2
}
}
我也做了
{
"Id": 20,
"TypeOfPallet": 0,
"Length": 20,
"Width": 40,
"Height": 50,
"Weight": 120,
"Damage": true,
"Shipment: 1,
"Customer: 2
}
在调试时甚至无法命中断点,我迷路了。
【问题讨论】:
标签: c# asp.net-mvc .net-core postman