我一直在关注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()
. 为什么这不能按预期工作?