How to use ASP.Net Web API to send a list of keys and return a list of key/values?

asp.net, asp.net-web-api, rest

Solution

With query string values, you can associate multiple values with a single field

public class ValuesController : ApiController
{
    protected static IList<Product> productList;
    static ValuesController()
    {
        productList = new List<Product>()
        {
            new Product() { Id = 1, Name = "Gizmo 1", Price = 1.99M },
            new Product() { Id = 2, Name = "Gizmo 2", Price = 2.99M },
            new Product() { Id = 3, Name = "Gizmo 3", Price = 3.99M }
        };
    }                
    public IEnumerable<Product> Get(IEnumerable<int> idList)
    {
        return productList;
    }
}

with the default routes, you can now make a GET request to the following endpoint

/api/values/FilterList?idList=1&idList=2

Problem

I'm new to REST services and have been working through the examples for ASP.Net Web API. What I would like to do is expand on this Get Method: ``` [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class ProductsController : ApiController { public IEnumerable<Product> GetAllProducts() { return new List<Product> { new Product() { Id = 1, Name = "Gizmo 1", Price = 1.99M }, new Product() { Id = 2, Name = "Gizmo 2", Price = 2.99M }, new Product() { Id = 3, Name = "Gizmo 3", Price = 3.99M } }; } ... ``` To something where I send a list of products and all the prices are returned, in concept it would look like this: ``` public IEnumerable<Product> GetProducts(string[] ProductNames) { var pList = new List<Product>; foreach (var s in ProductNames) { //Lookup price var LookedupPrice = //get value from a data source pList.Add(new Product() { Id = x, Name = s, Price = LookedUpPrice }); } return pList; } ``` Any ideas, and what would the REST call look like? I was thinking that I need to pass in a JSON object, but really am not sure.

Original source

Related problems