I would like to know what's the best way to design multiple updates to the resource(s) in WebApi .Net Core.
For example, I would like to enable the following features for users
resources
- Update User Password
- Update User role
- Update User Details (such as Firstname, last name, etc.)
So, according to REST tutorials and articles, I learnt that I need to use PATCH
method to update partial resource.
We did some discussions in the team and we are confused with these two options:
Option 1
implement multiple PATCH routes for different actions
- PATCH
/api/users/{id}/password
- PATCH
/api/users/{id}/role
- PATCH
/api/users/{id}/details
Option 2
implement ONLY single PATCH action for the whole resource. Users will send application/json-patch+json for partial updates.
- PATCH
/api/users/id
(acceptsJsonPatchDocument
parameter)
I tried to find the best practices for Restful Route Namings and most of them only cover for simple CRUD activities or nested resources.
For this kind of multiple UPDATE operations, may I know what's the best practice for naming the routes? Or the term for it to study in-depth? Thanks.
PATCH
request allows for better authorization control. You can annotate each method with anAuthorizeAttribute
with this approach to make sure only the fields you want to be updated are actually updated by authorized users accordingly (e.g. only admins are allowed to update user role, or in general: only <other user group> is allowed to update <field>) - user8574318