我一直在關注http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api並創建以下控制器後,我收到意想不到的結果...操作在MVC 4的WebAPI
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 /產品/ GetAllProducts
但是,這是行不通的。相反,我可以簡單地調用:
/API /產品
這確實會GetAllProducts()
描述的過程。爲什麼不按預期工作?
細節開始行動這裏:[ASP.NET Web API中的路由](http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api) – Alex
謝謝,@Alex我正在尋找類似的東西來添加。 – Joe