【发布时间】:2013-12-29 22:43:06
【问题描述】:
我一直关注http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api,在创建以下控制器后,我得到了意想不到的结果...
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
}
我希望能够像这样拨打电话:
/api/products/GetAllProducts
但这不起作用。相反,我可以简单地调用:
/api/产品
实际上执行GetAllProducts() 中描述的过程。为什么这没有按预期工作?
【问题讨论】:
标签: c# asp.net-mvc-4 asp.net-web-api