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

I am new to REST services and am working on examples for the ASP.Net Web API. What I would like to do is extend 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 }
        };
    } ...

Something where I send the list of products and all prices come back, in concept it will 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 a REST call look like? I thought I needed to pass a JSON object, but I'm not really sure.

+3
source share
1 answer

For query string values, you can associate multiple values ​​with one 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;
    }
}

using the default routes, you can now make a GET request to the next endpoint

/ API / value / FilterList idlist = 1 & amp ;? Id_list = 2

+4

All Articles