Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array of integers to WebAPI method in URI params?

I have the following:

    [HttpDelete]
    public HttpResponseMessage DeleteFolder(int[] ids)
    {

and I'm trying to use this:

DELETE http://localhost:24144/api/folder/1483;
DELETE http://localhost:24144/api/folder/[1483]

but these are coming up as null within the delete method - how do I send the data without putting it in the body (since this is a DELETE request)?

My routing has this:

            routes.MapHttpRoute(
           name: "Folder",
           routeTemplate: "api/folder/{id}",
           defaults: new { controller = "Folder", id = RouteParameter.Optional }
            );
like image 286
SB2055 Avatar asked Jul 22 '13 02:07

SB2055


People also ask

Can we pass array in params?

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

How do I pass multiple parameters to Web API controller methods?

Step1: Create a Blank Solution called WebAPI-Multiple-Objects as shown below. Add a new class library project (Model) into it and create two classes named Product, Supplier. Now, on the API controller side, you will get your complex types as shown below.

What are the different ways to pass parameters in Web API?

Passing Data in your API Calls REST API endpoints can pass data within their requests through 4 types of parameters: Header, Path, Query String, or in the Request Body.


1 Answers

Nevermind, I found this:

http://blog.codelab.co.nz/2012/10/16/passing-arrays-into-asp-net-web-api-as-parameters/

Couldn't find an answer on SO though so I'll leave it here.

Exerpt from the above linked page:

[HttpGet()]
public HttpResponseMessage FindByMembers([FromUri]Int32[] ids = null)
{
   //Do stuff
    return Request.CreateResponseMessage(HttpStatusCode.OK);
}

The Url will be http://mywebsite/api/mycontroller/findbymembers/?ids=1&ids=2&ids=3.

like image 131
SB2055 Avatar answered Oct 28 '22 01:10

SB2055