【发布时间】:2012-04-12 22:28:30
【问题描述】:
我是 REST 服务的新手,一直在研究 ASP.Net Web API 的示例。我想做的是扩展这个 Get 方法:
[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 对象,但真的不确定。
【问题讨论】:
标签: asp.net rest asp.net-web-api