C
C#13mo ago
Mee6

Endpoint routing how to support users/me and users/<id>

Hey, how do I support both cases? Do i create multiple controllers?
4 Replies
Mee6
Mee6OP13mo ago
for example: /users/me/items will get the items from the current user thanks to JWT, but how at the same time can I support /users/<id>/items? Multiple controllers with different routes?
Pobiega
Pobiega13mo ago
dont need multiple controllers, but you will need multiple actions
KuumaKoira
KuumaKoira13mo ago
You can do it in single action in a controller. Something like that (sorry if code will look clumsy, writing it on phone)
[HttpGet("users/{id}/items")]
Public async Task<IActionResult> (string id)
{
if(id == "me") { /* get id of current user from jwt token and items from user with given id*/}
else { /* get items of user with given id */

//Rest of controller body
}
[HttpGet("users/{id}/items")]
Public async Task<IActionResult> (string id)
{
if(id == "me") { /* get id of current user from jwt token and items from user with given id*/}
else { /* get items of user with given id */

//Rest of controller body
}
Would look like that in very simple example
Pobiega
Pobiega13mo ago
Please don't do that. It now allows users/meep/items too me is a special route value. allow it by a special action, and have another action take {id:int} do little to no logic in your actions, instead call shared methods that contain the actual logic there
Want results from more Discord servers?
Add your server