我如何通过请求中的字符串列表?

问题描述 投票:0回答:2
[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);

c# rest api refit
2个回答
17
投票
[FromUri]

属性。希望它能帮助某人!

    

确定您如何调用API的端点。但是,您是否尝试在方法的参数中使用Fromuri属性?

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

然后,您应该能够这样打电话:

?ids=11&ids=12&ids=13

甚至通过JavaScript通过一系列字符串。

1
投票

如果要将其设置为与接口相关的所有集合的默认值,则添加到索非亚的答案中

RefitSettings

source

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.