作者james999 (无伤大雅)
看板C_Sharp
标题[问题] ASP.NET Web API 2参数如何接收array
时间Fri Nov 10 19:50:03 2017
目前正在制作Web API 2的WebService,
GET, POST都能够顺利收到前端传过来的资料,
不过前端现在需求有可能会要传相同的资料数笔,
如:一次传多笔学生的个人资料..
我的方法如下:(示意)
[HttpPost]
public IHttpActionResult AddStudentData(AddStudentParams[] paramAddStudent)
不过检查paramAddStudent得到的Lenth为0,
是不是WebApiConfig.cs内的routing也要新增呢?
再麻烦板友回覆,网上查到关於传Array的routing设定很少..
谢谢。
--
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.34.236.60
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1510314607.A.9AD.html
1F:→ vi000246: 用List 11/10 21:10
2F:→ james999: 具体怎麽使用呢? 我直接指定array给List直接报错.. 11/12 14:19
代码
--
[HttpPost]
public int AssignMemberToGroup(int[] paramGo)
{
int total = 0;
var results = new List<int>();
for (var i = 0; i < paramGo.Length; i++)
{
var temp = new int();
temp = paramGo[i];
results.Add(temp);
}
return total;
}
--
参照网上搜到的...
https://stackoverflow.com/questions/34314306/webapi-passing-an-array-of-values
※ 编辑: james999 (1.34.236.60), 11/12/2017 14:35:04
3F:推 Wermut: 试试看([FromBody]AddStudentParams[] paramAddStudent) 11/13 06:06