如何在请求中传递字符串列表?

Sof*_* Bo 4 c# api rest refit

我正在为我的应用程序使用 Refit 库,我需要调用另一个服务。我需要获取所有带有我传递的 ID 的实体。

我尝试了 [Body] 属性,但它仍然不起作用。我设法传递了一个请求,但如果另一个服务获得的 id 列表为空,而我肯定传递了现有的 IEnumerable。

我的 IRefitProxy:

[Get("/students/allByIds")]
Task<IEnumerable<Student>> GetStudentsById(IEnumerable<string> ids);
Run Code Online (Sandbox Code Playgroud)

另一个服务的 API:

[RoutePrefix("api/students")]
[Route("allByIds")]
[HttpGet]
public IEnumerable<Student> AllByIds(IEnumerable<string> ids)
{
//ids here is null!

//call my repository blablabla
return students;
}
Run Code Online (Sandbox Code Playgroud)

我传递了一个数组/字符串列表,它为空。路径没问题,因为我设法陷入了带断点的方法。我怎样才能正确地通过它?

Sof*_* Bo 7

我设法解决了这个问题。添加[Query(CollectionFormat.Multi)]解决了问题。

[Get("/students/allByIds")] Task<IEnumerable<Student>>GetStudentsById([Query(CollectionFormat.Multi)]IEnumerable<string> ids);

接收 API 需要有[FromUri]属性。希望它可以帮助某人!