gpt4 book ai didi

c# - 下面的 PUT 方法代码中是否需要 int id?

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:57 24 4
gpt4 key购买 nike

我是 asp.net core 和 c# 的新手,但我不明白为什么我的 PUT 方法声明一个 id 属性并尝试将它与我的模型类中的 ID 属性绑定(bind)。为什么action不能直接调用ID属性?

    [HttpPut("{id}")]

public async Task<IActionResult> PutPatient(int id, Patient patient)

{
if (id != patient.PatientId)

{

return BadRequest();

}

最佳答案

我认为这可能与防止创建重复记录有关。根据Microsoft REST API Guidelines (强调是我的):

POST requests are not idempotent. This means that two POST requests sent to a collection resource with exactly the same payload MAY lead to multiple items being created in that collection. This is often the case for insert operations on items with a server-side generated id.

For example, the following request:

POST https://api.contoso.com/v1.0/people

Would lead to a response indicating the location of the new collection item:

201 Created
Location: https://api.contoso.com/v1.0/people/123

And once executed again, would likely lead to another resource:

201 Created
Location: https://api.contoso.com/v1.0/people/124

While a PUT request would require the indication of the collection item with the corresponding key instead:

PUT https://api.contoso.com/v1.0/people/123

关于c# - 下面的 PUT 方法代码中是否需要 int id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57493919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com