我是 asp.net core 和 c# 的新手,但我不明白为什么我的 PUT 方法声明一个 id 属性并尝试将其与我的模型类中的 ID 属性绑定(bind)。为什么操作不能直接调用 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/