当前位置: 首页 > news >正文

做网站个体户执照/谷歌优化推广

做网站个体户执照,谷歌优化推广,做门户网站开发的技术,沪尚茗居装修价格怎样本篇是接着上一篇《ASP.NET WebApi 入门》来介绍的。 前言习惯说 CRUD操作,它的意思是"创建、 读取、 更新和删除"四个基本的数据库操作。许多 HTTP 服务通过REST的 Api 进行CRUD 操作。在本教程中,生成非常简单的 web API 来管理产品的列表。…

本篇是接着上一篇《ASP.NET WebApi 入门》来介绍的。

前言

     习惯说 CRUD操作,它的意思是"创建、 读取、 更新和删除"四个基本的数据库操作。许多 HTTP 服务通过REST的 Api 进行CRUD 操作。在本教程中,生成非常简单的 web API 来管理产品的列表。每个产品将包含名称、 价格和类别,再加上一个产品 id。访问Uri对应如下:

行动HTTP 方法相对 URI
获取所有产品的列表GET/api/Product
根据ID得到一个产品GET/api/Product/id
根据Category获取产品的列表GET/api/Product?Category=类别
创建一个新的产品POST/api/Product
根据ID更新一个产品PUT/api/Product/id
根据ID删除一个产品DELETE/api/Product/id
添加仓储库

      在Models文件夹下,创建IProductRepository.cs:      

using System.Collections.Generic;namespace ApiDemo01.Models
{/// <summary>仓储操作接口</summary>public interface IProductRepository{IEnumerable<Product> GetAll();Product Get(int id);Product Add(Product item);void Remove(int id);bool Update(Product item);}
}

  在Models文件夹下,创建ProductRepository.cs:

using System;
using System.Collections.Generic;namespace ApiDemo01.Models
{/// <summary>仓储操作实现</summary>public class ProductRepository : IProductRepository{private List<Product> products = new List<Product>();private int _nextId = 1;public ProductRepository(){Add(new Product { Name = "Tomato soup", Category = "Groceries", Price = 1.39M });Add(new Product { Name = "Yo-yo", Category = "Toys", Price = 3.75M });Add(new Product { Name = "Hammer", Category = "Hardware", Price = 16.99M });}public IEnumerable<Product> GetAll(){return products;}public Product Get(int id){return products.Find(p => p.ID == id);}public Product Add(Product item){if (item == null){throw new ArgumentNullException("item");}item.ID = _nextId++;products.Add(item);return item;}public void Remove(int id){products.RemoveAll(p => p.ID == id);}public bool Update(Product item){if (item == null){throw new ArgumentNullException("item");}int index = products.FindIndex(p => p.ID == item.ID);if (index == -1){return false;}products.RemoveAt(index);products.Add(item);return true;}}
}

  注:存储库中保持在本地内存中的列表。在实际的应用中,您将存储的数据在外部,任一数据库或在云存储中。存储库模式将使易于更改以后实施。

修改控制器

      由于是接着上一篇博文介绍的项目,所以这里修改Controllers文件下的ProductController.cs: 

using ApiDemo01.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;namespace ApiDemo01.Controllers
{public class ProductController : ApiController{//TODO:接口引用具体实例(后面介绍MEF,使得接口解除使用具体实现)static readonly IProductRepository repository = new ProductRepository();//获取所有产品public IEnumerable<Product> GetAllProducts(){return repository.GetAll();}//根据ID获取一个产品public Product GetProduct(int id){Product item = repository.Get(id);if (item == null){throw new HttpResponseException(HttpStatusCode.NotFound);}return item;}//根据类别获取产品列表public IEnumerable<Product> GetProductsByCategory(string category){return repository.GetAll().Where(p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));}//添加一个产品//默认情况下,Web API 框架将响应状态代码设置为 200 (OK)。//但根据 HTTP/1.1 协议中,当 POST 请求的结果在创造一种资源,服务器应该回复状态为 201 (已创建)。//服务器将创建一个资源,它应在响应的位置标题中包括的新的资源的 URI。//public Product PostProduct(Product item)//{//    item = repository.Add(item);//    return item;//}//对上面方法进行改进//返回类型现在是HttpResponseMessage。//通过返回HttpResponseMessage而不是产品,我们可以控制的 HTTP 响应消息,包括状态代码和位置标头的详细信息。public HttpResponseMessage PostProduct(Product item){item = repository.Add(item);//创建的HttpResponseMessage ,并自动将产品对象的序列化表示形式写入到身体火炭的响应消息。var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);string uri = Url.Link("DefaultApi", new { id = item.ID });response.Headers.Location = new Uri(uri);return response;}//根据ID更新一个产品public void PutProduct(int id, Product product){product.ID = id;if (!repository.Update(product)){throw new HttpResponseException(HttpStatusCode.NotFound);}}//根据ID删除一个产品public void DeleteProduct(int id){Product item = repository.Get(id);if (item == null){throw new HttpResponseException(HttpStatusCode.NotFound);}repository.Remove(id);}}#region 此段代码是上一篇博文里介绍的方式,这里注释掉//public class ProductController : ApiController//{//    //模拟数据//    List<Product> pList = new List<Product>//    { //        new Product{ID=1, Name="Dell", Category="电脑" , Price=3500 },//        new Product{ID=2, Name="Apple", Category="手机" , Price=5500 },//        new Product{ID=3, Name="HP", Category="电脑" , Price=3000 }//    };//    //获取产品集合//    public IEnumerable<Product> GetProducts()//    {//        return pList;//    }//    //根据产品ID获取一个产品//    public IHttpActionResult GetProduct(int id)//    {//        var product = pList.FirstOrDefault((p) => p.ID == id);//        if (product == null)//        {//            return NotFound();//        }//        return Ok(product);//    }//} #endregion
}

  注:以上采用封装数据库仓储操作方式!

小结

      通过前面的介绍,似乎WebApi应用并不难。其实,很多高级使用(如:OData)还没涉及到讲解。有人说,WebApi和WebService很像,似乎有点为了“淘汰”WCF技术而生。但没有WCF强大,只是更容易使用。

       好了。这节内容并没有演示如何界面展示!我也是正在学习这项技术当中,本着先过一边官方的例子后,再弄一个完整的DEMO!

转载于:https://www.cnblogs.com/fanjibao/p/3726193.html

http://www.jmfq.cn/news/4919167.html

相关文章:

  • 中纪委网站两学一做征文/西安网红
  • 河北先进网站建设风格/seo到底是什么
  • 可以和外国人做朋友的网站/竞价推广托管服务
  • 做网站用boot/搜索app下载安装
  • 手机企业网站制作/网站优化提升排名
  • 中央政府网站的建设的意见/百度下载软件
  • 做外贸网站能用虚拟主机吗/中小企业网站优化
  • 大学物流仓储作业代做网站/政府免费培训 面点班
  • 西安最新公告/网站如何做关键词优化
  • 网站开发公司徐州/产品推广计划
  • 南京网站设计制作/常见的网络推广方法有哪些
  • 网站布局策划/怎么做游戏推广员
  • 利用网盘做视频网站/山西网络推广
  • 常用软件开发模型/seo关键词排名优化品牌
  • wordpress汽车模板下载/seo推广人员
  • 动易网站怎么进入后台/市场营销十大经典案例
  • 申请网站步骤/深圳seo博客
  • 做庭院的网站/百度知道app官方下载
  • vps自带ie浏览器不能访问网站/公司网站建设哪家公司好
  • 新网站如何做sem/成功的品牌推广案例分析
  • 网站开发css框架/汕头seo排名公司
  • 怎么搞免费的网站/企业网站定制
  • 高级又小众的公众号/企业网站优化推广
  • 网站快速优化排名/品牌seo推广
  • 给网站做镜像/广州:推动优化防控措施落
  • asp网站 被插入/国际域名注册网站
  • 深圳住房和建设管理局官方网站/搜索关键词推荐
  • 用php做购物网站视频/百度推广话术全流程
  • 书画网站模板/黑帽seo之搜索引擎
  • 做网站购买服务器多少钱/互联网营销师考试