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

标签 c# rest api refit

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

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

我的 IRefitProxy:

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

另一个服务的 API:
[RoutePrefix("api/students")]
[Route("allByIds")]
[HttpGet]
public IEnumerable<Student> AllByIds(IEnumerable<string> ids)
{
//ids here is null!

//call my repository blablabla
return students;
}

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

最佳答案

我设法解决了这个问题。添加 [Query(CollectionFormat.Multi)]解决了这个问题。
[Get("/students/allByIds")] Task<IEnumerable<Student>>GetStudentsById([Query(CollectionFormat.Multi)]IEnumerable<string> ids);
接收API需要有[FromUri]属性。希望它可以帮助某人!

关于c# - 如何在请求中传递字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56938495/

相关文章:

python - 使用 Python 向 mailchimp API 发送 POST 请求

Guid 的 C# 正则表达式

c# - CEFsharp 浏览器等待网站完全加载

java - 如何在 spring-mvc 中为 REST 查询提供对象列表?

java - 在 Spring 4.0.8.RELEASE + Hibernate 4.3.7.Final + 2.3.0 com.fasterxml.jackson 中使用 Hibernate4Module 时,其余请求给出 400 错误请求

api - 开源项目但对 API key 保密

c# - C#解析某个字符串

c# - 对应C# Control.SuspendLayout/ResumeLayout的swing方法

javascript - 将此 REST GET 请求集成到 JQuery 或 JavaScript 中

java - 在服务端不添加跨源代码如何解决跨源问题?