HttpStatus 如果 REST 路由中的资源 ID 与负载中的 ID 不同

HttpStatus if resource id in REST route differs from id in payload

当路由中的 ID 与请求正文中包含的资源中的 ID 不匹配时,哪种 http 状态最适合拒绝 (PUT) 请求?

示例:

[HttpPut("/api/todos/{id}")]
public IActionResult Update(int id, TodoItem item) {
if (item.id != id) {
  throw new HttpException(4??);
}

409状态码似乎是一个合理的选择:它用于表示请求冲突服务器上资源的当前状态。 不匹配 URL 和有效负载中的标识符之间的 不匹配 无论如何都是 冲突 .

响应应包含所有必要的信息,以便客户端识别冲突的来源,然后能够重新提交请求。对于在 Web API 中报告的问题,我建议您检查 RFC 7807.


查看 409 状态代码如何在 RFC 7231 中定义:

6.5.8. 409 Conflict

The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict. [...]