学习篇一:Web API

第一章:初识

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
参照网址:
在Web Api中强制使用Https

1.1 入门实例

1.1.1 创建MVC项目,HelloWebAPI

1.1.2 添加一个Model,product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HelloWebAPI.Models
{
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }
}
1.1.3 添加一个controller
using HelloWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace HelloWebAPI.Controllers
{
    public class ProductsController : ApiController
    {
        //
        // GET: /Products/

        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;
        }
        public IEnumerable<Product> GetProductsByCategory(string category)
        {
            return products.Where(
                (p) => string.Equals(p.Category, category,
                    StringComparison.OrdinalIgnoreCase));
        }

    }
}

1.2 知识点整理

1.2.1 如何修改请求方式

我们可以看到在Action 中没有使用[HttpGet]、[HttpPost] 等修饰,那究竟它是如何运作的呢?
  Action 皆以HTTP 动词开头Get、Post、Put、Delete ,这个也是刚好符合 webapi的约定的。
  你调用什么类型的方法 ,例如 post 方法,那么他就去 你的所有的 action 里面 去找 以 post 开头的方法 ,名字可以随便叫,例如 postToDataBase 等等,只要开头匹配 就可以了
例如:在上面的例子中:

public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

当改成PostAllProducts时,则以Post的请求方式。



方法名虽然带Get,Post,但是在请求的的时候并不用加上。
有几种标准的请求,如get,post,put,delete等,它们分别对应的几个操作,下面讲一下:

  • GET:生到数据列表(默认),或者得到一条实体数据
  • POST:添加服务端添加一条记录,记录实体为Form对象
  • PUT:添加或修改服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输
  • DELETE:删除 服务端的一条记录
1.2.2 如何传递参数?

在方法里直接添加参数,然后运行那程序,可以看到:




当然这里的1,2并没有实际意义。再看看传递一个实体看看:




原来的文件:(由于里面存在一些不必要的东西,试试删除一些,简化一点)

修改后的文件结构:(不喜欢启动时什么都没有,添加了一个index.html,修改下路由配置)

启动:



数据接口暂时告一段落,然后接下来学习EF 的code first的学习……

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,028评论 19 139
  • API定义规范 本规范设计基于如下使用场景: 请求频率不是非常高:如果产品的使用周期内请求频率非常高,建议使用双通...
    有涯逐无涯阅读 7,784评论 0 6
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,786评论 18 399
  • 生活中、工作中,你一定会遇到这样的人,只要他有点小钱了,他就到处炫耀,说自己有多少套房子;看了一点书籍了,就觉得自...
    戴老师成长记录仪阅读 4,935评论 2 5
  • 像看生活一样看小说,像看小说一样看生活————题记 说起看小说,仔细回忆,应该是从高一开始的吧,看的第一...
    小盈得满阅读 2,286评论 1 3

友情链接更多精彩内容