[問題] ASP.NET Web API 2參數如何接收array

看板C_Sharp (C#)作者 (無傷大雅)時間7年前 (2017/11/10 19:50), 7年前編輯推噓1(100)
留言1則, 1人參與, 7年前最新討論串1/1
目前正在製作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://www.ptt.cc/bbs/C_Sharp/M.1510314607.A.9AD.html

11/10 21:10, 7年前 , 1F
用List
11/10 21:10, 1F

11/12 14:19, 7年前 , 2F
具體怎麼使用呢? 我直接指定array給List直接報錯..
11/12 14:19, 2F
代碼 -- [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

11/13 06:06, 7年前 , 3F
試試看([FromBody]AddStudentParams[] paramAddStudent)
11/13 06:06, 3F
文章代碼(AID): #1Q1P9lcj (C_Sharp)
文章代碼(AID): #1Q1P9lcj (C_Sharp)