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

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

    Easy way to send array params to web api

    API

    public IEnumerable GetCategories([FromUri]int[] categoryIds){
     // code to retrieve categories from database
    }
    

    Jquery : send JSON object as request params

    $.get('api/categories/GetCategories',{categoryIds:[1,2,3,4]}).done(function(response){
    console.log(response);
    //success response
    });
    

    It will generate your request URL like ../api/categories/GetCategories?categoryIds=1&categoryIds=2&categoryIds=3&categoryIds=4

提交回复
热议问题