The Microsoft.AspNetCore.Mvc.ControllerBase comes with a method RedirectToAction(string? actionName, string? controllerName, object? routeValues). Returning the result of this method will redirect the requester to another action using a status 302 Found. Actions on the MeController are essentially wrappers for for accessing user-owned resources by parsing the authorized user id, so this is a much more sensible way compared to what we currently do by re-using the related service method for obtaining the resource.
For example, consider the endpoint GET/me. This endpoint currently parses the requester's authorized user id, and then proceeds to reimplement the functionality of GET/users/{id}.
public async Task<IActionResult> GetAsync()
{
var id = HttpContext.AuthorizedUserIdentity();
if (!id.HasValue)
{
return Unauthorized();
}
UserDTO? user = await _userService.GetAsync(id.Value);
if (user?.OsuId == null)
{
return NotFound();
}
return Ok(user);
}
Here is what it could look like using RedirectToAction()
public IActionResult Get()
{
var id = HttpContext.AuthorizedUserIdentity();
if (!id.HasValue)
{
return Unauthorized();
}
return RedirectToAction("Get", "Users", new { id });
}
The
Microsoft.AspNetCore.Mvc.ControllerBase
comes with a methodRedirectToAction(string? actionName, string? controllerName, object? routeValues)
. Returning the result of this method will redirect the requester to another action using a status302 Found
. Actions on the MeController are essentially wrappers for for accessing user-owned resources by parsing the authorized user id, so this is a much more sensible way compared to what we currently do by re-using the related service method for obtaining the resource.For example, consider the endpoint
GET
/me
. This endpoint currently parses the requester's authorized user id, and then proceeds to reimplement the functionality ofGET
/users/{id}
.Here is what it could look like using
RedirectToAction()