【问题标题】:Unable to Postman POST to dot net API when Foreign Keys are involved c#当涉及外键时,无法邮递员 POST 到 dot net API c#
【发布时间】: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


    【解决方案1】:

    如果在您尝试使用 POSTMAN 发出 POST 请求时断点没有停止您的应用程序,则可能是您的请求出错了。

    我的意思是,例如,当您向服务器发送 GET 请求时 - 没有实现此方法的控制器会抛出此错误,因为它没有找到任何 GET 方法。

    您能否发布您的邮递员请求的屏幕截图?

    【讨论】:

      【解决方案2】:

      移除 [Bind] 并添加 [FromBody] 以在 Postman 中测试

      public async Task<IActionResult> CargoPost([FromBody]Cargo cargo)
      

      使用此数据进行测试:

      {
      "Id": 20,
      "TypeOfPallet": 0,
      "Length": 20,
      "Width": 40,
      "Height": 50,
      "Weight": 120,
      "Damage": true
      }
         
      

      并修复货物

      ....
       public int? ShipmentId { get; set; }
          public virtual Shipment Shipment { get; set; }
        public int? CustomerId { get; set; }
          public virtual Customer Customer { get; set; }
      

      之后你也可以测试你的数据

      {
      "Id": 20,
      "TypeOfPallet": 0,
      "Length": 20,
      "Width": 40,
      "Height": 50,
      "Weight": 120,
      "Damage": true,
      "ShipmentId": 1,
      "CustomerId": 2
      }
      

      【讨论】:

      • 成功了,非常感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2021-07-03
      相关资源
      最近更新 更多