[英]Best way to work with list of objects on Http Patch
in my scenario I have a web client and an rest api (.NET Core) in web client has a page that list all Clients, in this list the user can select many Clients and mark them as Enabled. 如何在我的 api 中處理“標記為啟用”? 我可以在方法 PATCH 上一一更新,但使用列表我不知道如何。
public IActionResult Patch( long id, [FromBody] JsonPatchDocument<Client> patch )
{
var client = base.DataContext.Clients.Find( id );
if( client != null )
patch.ApplyTo( client, ModelState );
return Ok();
}
public class Client
{
public string Name { get; set; }
public string Description { get; set; }
public bool Enabled { get; set; }
}
補丁列表的HttpPatch
動作與Object相同。
[HttpPatch]
public IActionResult JsonPatchForClients([FromBody] JsonPatchDocument<List<Client>> patchDoc)
{
var clients = CreateClients();
patchDoc.ApplyTo(clients);
return new ObjectResult(clients);
}
private List<Client> CreateClients()
{
return new List<Client>()
{
new Client
{
Name = "Client0",
Description = "Description0",
Enabled = true
},
new Client
{
Name = "Client1",
Description = "Description1",
Enabled = true
},
new Client
{
Name = "Client2",
Description = "Description2",
Enabled = true
}
};
}
public class Client
{
public string Name { get; set; }
public string Description { get; set; }
public bool Enabled { get; set; }
}
接下來,我將詳細介紹如何發送Patch
Body。
這是原始列表:
[
{
"name": "Client0",
"description": "Description0",
"enabled": true
},
{
"name": "Client1",
"description": "Description1",
"enabled": true
},
{
"name": "Client2",
"description": "Description2",
"enabled": true
}
]
1.添加到“ /-
”數組的末尾並添加到“ /0
”數組的第一個
[
{
"op": "add",
"path": "/-",
"value": {
"name": "Order3",
"description": "Description3",
"enabled": true
}
}
]
結果:
[
{
"name": "Client0",
"description": "Description0",
"enabled": true
},
{
"name": "Client1",
"description": "Description1",
"enabled": true
},
{
"name": "Client2",
"description": "Description2",
"enabled": true
},
{
"name": "Order3",
"description": "Description3",
"enabled": true
}
]
2.將Order3
替換為Order0
[
{
"op": "replace",
"path": "/0",
"value": {
"name": "Order3",
"description": "Description3",
"enabled": true
}
}
]
結果:
[
{
"name": "Order3",
"description": "Description3",
"enabled": true
},
{
"name": "Client1",
"description": "Description1",
"enabled": true
},
{
"name": "Client2",
"description": "Description2",
"enabled": true
}
]
3.刪除客戶端Client0
[
{
"op": "remove",
"path": "/0"
}
]
結果:
[
{
"name": "Client1",
"description": "Description1",
"enabled": true
},
{
"name": "Client2",
"description": "Description2",
"enabled": true
}
]
move
、 copy
、 test
)。另一種方法是全部replace
。
[HttpPatch]
public IActionResult JsonPatchForClients([FromBody] JsonPatchDocument patchDoc)
{
var clients = new Clients();
patchDoc.ApplyTo(clients);
return new ObjectResult(clients);
}
public class Clients
{
public List<Client> clients { get; set; }
public Clients()
{
clients = new List<Client>()
{
new Client
{
Name = "Client0",
Description = "Description0",
Enabled = true
},
new Client
{
Name = "Client1",
Description = "Description1",
Enabled = true
},
new Client
{
Name = "Client2",
Description = "Description2",
Enabled = true
}
};
}
}
JSONPatch 的主體:
[
{
"op": "replace",
"path": "/clients",
"value": [
{
"name": "clients1",
"description": "Desc1",
"enabled": false
},{
"name": "clients2",
"description": "Desc2",
"enabled": false
}
]
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.