RESTful API 端点命名

我正在开发一个公开 RESTful API 的 asp.net core 2.2 后端。


当前的实现工作正常(为清楚起见删除了额外的代码):


namespace Sppd.TeamTuner.Controllers

{

    [Authorize]

    [ApiController]

    [Route("[controller]")]

    public class UsersController : ControllerBase

    {

        private readonly ITeamTunerUserService _userService;

        private readonly ITokenProvider _tokenProvider;

        private readonly IAuthorizationService _authorizationService;

        private readonly IMapper _mapper;


        public UsersController(ITeamTunerUserService userService, ITokenProvider tokenProvider, IAuthorizationService authorizationService, IMapper mapper)

        {

            _userService = userService;

            _tokenProvider = tokenProvider;

            _authorizationService = authorizationService;

            _mapper = mapper;

        }


        [HttpGet]

        public async Task<IActionResult> GetByUserId(Guid userId)

        {

            // TODO: secure this call

            var user = _userService.GetByIdAsync(userId);

            return Ok(_mapper.Map<UserDto>(await user));

        }

    }

}

单个 API 方法适用于 URL https://localhost:5001/Users?userId=4AF29C4A-282A-4FB8-8691-9D44398A97F2


现在我想添加第二种方法:


[HttpGet]

public async Task<IActionResult> GetByTeamId(Guid teamId)

{

    // TODO: secure this call

    var users = _userService.GetByTeamIdAsync(teamId);

    return Ok(_mapper.Map<IEnumerable<UserDto>>(await users));

}

这将导致 URL https://localhost:5001/Users?teamId=4AF29C4A-282A-4FB8-8691-9D44398A97F2(请注意,与第一次调用相比,参数是 teamId 而不是 userId)。

问题:

- 这是一个 SwaggerUI 问题吗?意思是控制器会以其他方式工作吗?

- 用户或团队控制器是否应该公开获取所有团队用户的方法?我们按团队 ID 查询并返回用户。

- 如果它保留在用户控制器中,那么拥有唯一端点的“最佳”方式是什么?


肥皂起泡泡
浏览 98回答 4
4回答

森栏

我会把它放在Teams控制器中并使用类似路由的方式接收它/teams/1/users- 其中 1 是团队 ID。这更像是“休息”:)。您需要使用注释您的 Teams 控制器操作[Route("{teamId}/users")].但是,如果您想将其保留在用户控制器中,则使用注释团队操作[Route("some-action-name/{teamId}")]并使用users/some-action-name/{teamId}

尚方宝剑之说

你可以Attribute Routing试一试,这就是我的意思[HttpGet("/{userId}")]public async Task<IActionResult> GetByUserId(Guid userId)[HttpGet("/{teamId}")]public async Task<IActionResult> GetByTeamId(Guid teamId)[HttpGet("/{teamId}")]route用参数定义一个新的teamId

慕田峪9158850

you&nbsp;need&nbsp;to&nbsp;provide&nbsp;action&nbsp;name&nbsp;in&nbsp;your&nbsp;url.which&nbsp;is&nbsp;unique&nbsp;path像这样&nbsp;https://localhost:5001/Users/GetByTeamId?teamId=4AF29C4A-282A-4FB8-8691-9D44398A97F2

慕桂英546537

尝试向您的路由属性添加操作,不要忘记将操作名称添加到您的网址namespace Sppd.TeamTuner.Controllers{&nbsp; &nbsp;[Authorize]&nbsp; &nbsp;[ApiController]&nbsp; &nbsp;[Route("[controller]/[Action]")]&nbsp;public class UsersController : ControllerBase&nbsp;{&nbsp; &nbsp; &nbsp;private readonly ITeamTunerUserService _userService;&nbsp; &nbsp; &nbsp;private readonly ITokenProvider _tokenProvider;&nbsp; &nbsp; &nbsp;private readonly IAuthorizationService _authorizationService;&nbsp; &nbsp; &nbsp;private readonly IMapper _mapper;&nbsp; &nbsp; &nbsp;public UsersController(ITeamTunerUserService userService, ITokenProvider tokenProvider, IAuthorizationService authorizationService, IMapper mapper)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _userService = userService;&nbsp; &nbsp; &nbsp; &nbsp; _tokenProvider = tokenProvider;&nbsp; &nbsp; &nbsp; &nbsp; _authorizationService = authorizationService;&nbsp; &nbsp; &nbsp; &nbsp; _mapper = mapper;&nbsp; &nbsp; }&nbsp; &nbsp; [HttpGet]&nbsp; &nbsp; public async Task<IActionResult> GetByUserId(Guid userId)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // TODO: secure this call&nbsp; &nbsp; &nbsp; &nbsp; var user = _userService.GetByIdAsync(userId);&nbsp; &nbsp; &nbsp; &nbsp; return Ok(_mapper.Map<UserDto>(await user));&nbsp; &nbsp; }&nbsp; }}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP