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

标签 c# asp.net-core model-view-controller

我是 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/

相关文章:

jquery - MVC 使用 JQuery 更新模型

c# - Linq to Object/XML 其中元素不存在

C# 或 C++ 沙盒程序集

c# - 如何使用 Watin IE 处理 Windows 安全警报对话框

c# - 在 ASP.NET CORE 中流代理实时流

database - 在 ASP.NET Core MVC 6 中记录到数据库

javascript - MVC中模型的异步更新

c# - 在 Microsoft Visual Studio 2013 中创建新项目时出错

c# - 在 Controller (ASP.NET Core)中进行 DI 时如何在 SignalR 中使用 Clients.Caller 和 Clients.Others?

java - Spring Boot 可以设置从 Controller 端点下载的文件的名称吗?