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

前端 未结 17 2136
孤独总比滥情好
孤独总比滥情好 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:06

    In case someone would need - to achieve same or similar thing(like delete) via POST instead of FromUri, use FromBody and on client side(JS/jQuery) format param as $.param({ '': categoryids }, true)

    c#:

    public IHttpActionResult Remove([FromBody] int[] categoryIds)
    

    jQuery:

    $.ajax({
            type: 'POST',
            data: $.param({ '': categoryids }, true),
            url: url,
    //...
    });
    

    The thing with $.param({ '': categoryids }, true) is that it .net will expect post body to contain urlencoded value like =1&=2&=3 without parameter name, and without brackets.

提交回复
热议问题