Pass an array of integers to ASP.NET Web API?

前端 未结 17 2058
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:40

I have an ASP.NET Web API (version 4) REST service where I need to pass an array of integers.

Here is my action method:



        
17条回答
  •  爱一瞬间的悲伤
    2020-11-22 05:23

    If you want to list/ array of integers easiest way to do this is accept the comma(,) separated list of string and convert it to list of integers.Do not forgot to mention [FromUri] attriubte.your url look like:

    ...?ID=71&accountID=1,2,3,289,56

    public HttpResponseMessage test([FromUri]int ID, [FromUri]string accountID)
    {
        List accountIdList = new List();
        string[] arrAccountId = accountId.Split(new char[] { ',' });
        for (var i = 0; i < arrAccountId.Length; i++)
        {
            try
            {
               accountIdList.Add(Int32.Parse(arrAccountId[i]));
            }
            catch (Exception)
            {
            }
        }
    }
    

提交回复
热议问题