0

I am a bit of a web API beginner so here goes. I have defined my CustomersController:

public class CustomersController : ApiController
{
    public Customer Get(int id)
    {
        var customer = CustomerDataSource.customerData.Where(c => c.id == id).First();
        return customer;
    }

    public List<Customer> Get(string search)
    {
        var list = CustomerDataSource.customerData;
        return list;
    }


}

The CustomerDataSource.customerData returns a list of objects :

public static List<Customer> customerData
{
    get
    {
        Customer customer1 = new Customer() {id=1, name = "Bert", address = "London"};
        customer1.nicknames= new string[]{"jack","bart"};

        Customer customer2 = new Customer() { id=2,name = "Jon", address = "New York" };
        customer2.nicknames= new string[]{"ed","fred"};

        List<Customer> listCustomers = new List<Customer>();
        listCustomers.Add(customer1);
        listCustomers.Add(customer2);

        return listCustomers;
    }
}
public class Customer
{   
        public int id { get; set; }
        public string name { get; set; }
        public string address { get; set; }
        public string[] nicknames { get; set; }
}

I have defined a Get method which returns all the instances of Customer fine. I can also add an extra search argument if needed in case I would like to search in my datasource. Suppose I would like to do another task like sorting for example. I tried:

public List<Customer> Get(string sort)
{
  var list = CustomerDataSource.customerData;
  return list;
}

But this wont compile:

'CustomersController' already defines a member called 'Get' with the same parameter

The question is how to refactor my controller so I can pass in a searchargument and a sortargument while still maintaining the web API interface?

Pindakaas
  • 4,389
  • 16
  • 48
  • 83
  • 1
    Possible duplicate of http://stackoverflow.com/questions/14353466/overload-web-api-action-method-based-on-parameter-type – G. Stoynev Jan 18 '14 at 02:44
  • In your case, you can have `Get(id)` to get one and `Get()` to get all. Then add to query string of `Get()`, `sort={column id}`. Parse this query string and ass to sort logic – T.S. Jan 20 '14 at 18:53

1 Answers1

0

Building on T.S.'s suggestion, you could add a post method to your controller and post a JSON model as seen below. You would have to build out the CustomerDataSource.GetCustomerData method of course.

public class CustomerSearchModel
{
    public string Search { get; set; }
    public string Sort { get; set; }
    public string Foo { get; set; }
}



public class CustomersController : ApiController
{
    public List<Customer> Get()
    {
        //If you wanted to just parse the querystring, do it here with Request.RequestUri.Query
        return CustomerDataSource.customerData;
    }

    public Customer Get(int id)
    {
        var customer = CustomerDataSource.customerData.Where(c => c.id == id).First();
        return customer;
    }

    public List<Customer> Post(CustomerSearchModel model)
    {
        return CustomerDataSource.GetCustomerData(model);
    }
}

If you need the model to be passed in from the URI, add [FromUri]:

 public List<Customer> Post([FromUri] CustomerSearchModel model)
Hink
  • 504
  • 8
  • 11