public class ProductsController : ApiController { // Mock product list public static List<Product> productList =http://www.mamicode.com/ initProductMockDataList(); private static List<Product> initProductMockDataList() { return new List<Product>() { new Product {ProductID=http://www.mamicode.com/1,ProductName=http://www.mamicode.com/"Product A",Price=http://www.mamicode.com/1000000,Count=http://www.mamicode.com/5,Description=http://www.mamicode.com/"Description A"}, new Product {ProductID=http://www.mamicode.com/2,ProductName=http://www.mamicode.com/"Product B",Price=http://www.mamicode.com/200000,Count=http://www.mamicode.com/2,Description=http://www.mamicode.com/"Description B"}, new Product {ProductID=http://www.mamicode.com/3,ProductName=http://www.mamicode.com/"Product C",Price=http://www.mamicode.com/500000,Count=http://www.mamicode.com/8,Description=http://www.mamicode.com/"Description C"}, new Product {ProductID=http://www.mamicode.com/4,ProductName=http://www.mamicode.com/"Product D",Price=http://www.mamicode.com/80000,Count=http://www.mamicode.com/10,Description=http://www.mamicode.com/"Description D"}, new Product {ProductID=http://www.mamicode.com/5,ProductName=http://www.mamicode.com/"Product E",Price=http://www.mamicode.com/300000,Count=http://www.mamicode.com/3,Description=http://www.mamicode.com/"Description E"} }; } public IEnumerable<Product> Get() { return productList; } public Product Get(int id) { return productList.Where(p => p.ProductID ==http://www.mamicode.com/ id).FirstOrDefault(); } public void Post([FromBody]Product product) { var lastProduct = productList.OrderByDescending(p => p.ProductID).FirstOrDefault(); int newProductID = lastProduct.ProductID + 1; product.ProductID =http://www.mamicode.com/ newProductID; productList.Add(product); } public void Put([FromBody]Product product) { var currentProduct = productList.Where(p => p.ProductID ==http://www.mamicode.com/ product.ProductID).FirstOrDefault(); if (currentProduct != null) { foreach (var item in productList) { if (item.ProductID.Equals(currentProduct.ProductID)) { item.ProductName =http://www.mamicode.com/ product.ProductName; item.Price =http://www.mamicode.com/ product.Price; item.Count =http://www.mamicode.com/ product.Count; item.Description =http://www.mamicode.com/ product.Description; } } } } public void Delete(int id) { Product product = productList.Where(p => p.ProductID ==http://www.mamicode.com/ id).FirstOrDefault(); productList.Remove(product); } }
,相关文章
- jQuery Ajax传递数组到asp.net web api参数为空
- ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料
- ASP.NET Web Api 实现数据的分页(转载)
- 通过HttpClient来调用Web Api接口,实体参数的传递
- ASP.NET Core MVC中构建Web API
- 如何在模板中生成包含现有文本的word文档docx,但是通过asp.net Web表单填写值?
- ASP.NET后台调用API方法
- 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署
- 在Mac下创建ASP.NET Core Web API
- 如何停止ASP.NET web api PUT动词返回500