我是REST服务的新手,并且一直在研究ASP.Net Web API的示例。我想要做的是扩展这个获取方法:
[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 }
};
} ...
在我发送产品清单并返回所有价格的地方,概念上看起来像这样:
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;
}
任何想法,REST调用会是什么样子?我以为我需要传入一个JSON对象,但实际上我不确定。
答案 0 :(得分:4)
使用查询字符串值,您可以将多个值与单个字段相关联
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;
}
}
使用默认路由,您现在可以向以下端点发出GET请求
/ API /值/ FilterList IDLIST = 1&安培; IDLIST = 2