c# - 尝试使用路由属性 web api 从 uri 绑定一个 int[] 数组(作为对象属性)
问题描述
我使用 .Net Framework 构建了一个 API,我有一个控制器
[RoutePrefix("api/student")]
public StudentController : ApiController
{
private readonly IService service;
}
public StudentController(IService service)
{
this.service = service
}
[HttpGet, Route("getStudents/{Ids:int}")]
public async Task<IHttpActionResult> GetStudents([FromUri]GetStudentsRequest request)
{
//bla bla bla
}
GetStudentsRequest 类是
public class GetStudentsRequest
{
public int[] Ids { get; set; }
}
我想在一个请求中传递多个 id,但问题是如何通过邮递员传递例如 3 个 id?
还是我必须更改属性?
我只想要来自 uri 而不是来自身体!
我试试这个将整数数组传递给 ASP.NET Web API? 排名最高的答案 localhost:XXXXX/api/student/getStudents?Ids=1&Ids=2 但没有用。不同的是我有一个具有数组属性的对象。
解决方案
最后我解决了。以供将来参考,如果有人将 int 数组作为请求对象中的属性,则必须修改
路由属性
正确的是
[HttpGet, Route("getStudents")] //i remove /{Ids:int}
public async Task<IHttpActionResult> GetStudents([FromUri]GetStudentsRequest request)
{
//bla bla bla
}
并且 uri 是 localhost:XXXXX/api/student/getStudents?Ids=1&Ids=2
如果有人有更好的解决方案,请发布!
推荐阅读
- javascript - 反应脚本构建服务工作者不缓存自定义文件
- ios - 片段着色器中的纹理读取
- google-analytics - Google Analytics 中的子目录跟踪
- javascript - 日期时间选择器标记值
- c# - 在 Foreach 循环中使用谓词生成器时遇到问题
- java - Vaadin Tabsheet 不希望刷新选项卡内容
- javascript - 如何使用 VueJS 动态添加属性?
- ruby - 我可以在 rbenv 中创建沙盒 gem 环境吗?
- ubuntu - 如何在 Ubuntu 16.04 上离线安装 Tensorflow 和 Cuda?
- import - SAP HANA:将数据从 CSV 文件插入/导入表的最高效方式