Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass array of an object to webapi

I have a .net mvc 4 webapi project that I'm trying to pass an array of an object to a method on my controller.

I've found some examples here on SO that talk about needing to set my object's properties with: param1=whatever&param2=bling&param3=blah.

But I don't see how I can pass in a collection using that.

Here is my method signature. Notice I've decorated the argument with the [FromUri] attribute.

public List<PhoneResult> GetPhoneNumbersByNumbers([FromUri] PhoneRequest[] id)
{
    List<PhoneResult> prs = new List<PhoneResult>();
    foreach (PhoneRequest pr in id)
    {
        prs.Add(PhoneNumberBL.GetSinglePhoneResult(pr.PhoneNumber, pr.RfiDate, pr.FinDate, pr.State));
    }
    return prs;
}

here is my simple PhoneRequest object:

public class PhoneRequest
{
    public string PhoneNumber { get; set; }
    public string RfiDate { get; set; }
    public string FinDate { get; set; }
    public string State { get; set; }
}

and here's a sample of what I'm using to pass in:

http://localhost:3610/api/phonenumber/getphonenumbersbynumbers/
   [{"PhoneNumber":"8016667777","RfiDate":"","FinDate":"2012-02-11","State":"UT"},
    {"PhoneNumber":"8018889999","RfiDate":"2012-12-01","FinDate":"","State":"UT"}]

using this comes back with "bad request"

I also tried this

http://localhost:3610/api/phonenumber/getphonenumbersbynumbers?
    id=[{"PhoneNumber":"8016667777","RfiDate":"","FinDate":"2012-02-11","State":"UT"},
        {"PhoneNumber":"8018889999","RfiDate":"2012-12-01","FinDate":"","State":"UT"}]

which does reach the method, but the array is null.

how can I pass in an array of my PhoneRequest object to my Web API method?

like image 533
Christopher Johnson Avatar asked Dec 19 '12 23:12

Christopher Johnson


People also ask

Can we use Frombody with Httpget?

A Get does not send the body of the form, it only requests a URL. Use a <form> in your view and post it to your controller method, which needs to be decorated with HttpPost.

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.

How does Web API bind ASPX parameter?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.


2 Answers

Try passing the PhoneRequest[] from the uri in this format:

http://localhost:3610/api/phonenumber/getphonenumbersbynumbers?
    id[0][PhoneNumber]=8016667777&id[0][FinDate]=2012-02-11&id[0][State]=UT&
    id[1][PhoneNumber]=8018889999&id[1][RfiDate]=2012-12-01&id[1][State]=UT
like image 109
Maggie Ying Avatar answered Sep 25 '22 02:09

Maggie Ying


I suggest you use POST for this.

As you query string grows, you will run into problems with the maximum length of the URL, which is browser dependent.

If you have a lot of parameters to pass, a POST is perfectly acceptable even if you are really only GETting data. What you will lose, however, is the ability for the user to bookmark a particular page with the query string.

like image 29
Yasser Shaikh Avatar answered Sep 23 '22 02:09

Yasser Shaikh