1 通过NuGet程序管理包添加  Microsoft Asp.Net webAPI 2.2 的引用

2 添加两个文件夹Controllers和Models

  2.1 在本地模拟数据库,所以在Models文件夹中添加Storages类  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MyAPIN.Models
{
public static class Storages
{
public static IEnumerable<Student> Students { get; set; }
public static IEnumerable<Teacher> Teachers { get; set; } static Storages()
{
Students = new List<Student>()
{
new Student {Id=,Name="逍遥小天狼1",Age=,Gender=false},
new Student {Id=,Name="逍遥小天狼2",Age=,Gender=false},
new Student {Id=,Name="逍遥小天狼3",Age=,Gender=false},
new Student {Id=,Name="逍遥小天狼4",Age=,Gender=false},
new Student {Id=,Name="逍遥小天狼5",Age=,Gender=false},
new Student {Id=,Name="逍遥小天狼6",Age=,Gender=false},
};
Teachers = new List<Teacher>();
} } public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool Gender { get; set; }
}
public class Student : Person { }
public class Teacher : Person { }
}

Storages

  2.2 同时添加StudentsController 和 TeacherController 在Controllers中

using MyAPIN.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http; namespace MyAPIN.Controllers
{
/// <summary>
/// 学生资源集合
/// </summary>
public class StudentsController : ApiController
{
//C R U D
public IEnumerable<Student> Get() {
return Storages.Students;
} public Student Get(string name) {
return Storages.Students.FirstOrDefault(s=>s.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase));
} }
}

StudentsController

3 添加Global 入口文件 用于配置API路由

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using System.Web.SessionState; namespace MyAPIN
{
public class Global : System.Web.HttpApplication
{ protected void Application_Start(object sender, EventArgs e)
{
//配置API路由
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
"default_api",
"{controller}/{item}",
new { item=RouteParameter.Optional});
} }
}

Global

运行效果

添加其他接口

using MyAPIN.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http; namespace MyAPIN.Controllers
{
/// <summary>
/// 学生资源集合
/// </summary>
public class StudentsController : ApiController
{
//C R U D
public IEnumerable<Student> Get() {
return Storages.Students;
} public Student Get(string item) {
return Storages.Students.FirstOrDefault(s=>s.Name.Equals(item,StringComparison.InvariantCultureIgnoreCase));
}
public void Post(Student entity) {
var list = Storages.Students as IList<Student>;
entity.Id = Storages.Students.Max(s=>s.Id)+;
list.Add(entity);
}
public void Delete([FromUri] string item) {
var entity = Get(item);
var list = Storages.Students as IList<Student>;
list.Remove(entity);
}
public void Put([FromUri] string item,[FromBody] Student entity) {
Delete(item);
Post(entity);
}
}
}

4 客戶端調用

添加"控制台應用程序" 引用web Api 2.2 Client

步步為營-98-MyAPI的更多相关文章

  1. 步步為營-97-MyMVC3

    說明: 解決另外一個不合理之處:通過控制器完成處理 1:在mvc文件夾下面添加一個工廠類文件DefaultControllerFactory 1.2進一步升級為抽象工廠 2 下一步如何規範Contro ...

  2. 步步為營-96-MyMVC2

    說明:地址欄中的URL還存在一些不足之處 地址欄中最好能是http"www.sss.com/asdf/aaa 1.1 模擬路由的原理:創建Routing文件夾,并添加URLRoutingMo ...

  3. 步步為營-95-MyMVC 1.0

    說明:通過自己編寫MyMVC以便於對MVC內容實現機制有更深刻的認識 1.1:創建MyMVC項目,刪除無關引用,只保留system 和 system.web.同時該項目中以後添加一些文件后也要刪除無關 ...

  4. From COM to COM 侯捷 1998.06.12

    摘要: 本文簡介 C++ Object Model 和 Component Object Model 的基本概念,並引介四本書籍: 1. Inside The C++ Object Model 2. ...

  5. 超时空英雄传说2复仇魔神完全攻略&秘技

    ╓─╥───────────────────────────────────────────────────╥─╖ ║ ║ 超 時 空 英 雄 傳 說 2 ║ ║ ║ ║ --復 仇 魔 神-- ║ ...

  6. BT觀念分享和常見問題彙整

    一. TCP/IP基本觀念 1. IP : 每台在TCP/IP網路上的電腦必須具備的一個代表號或一個地址.IP又分為private IP(192.168.x.x /10.x.x.x /172.16.x ...

  7. H TC並沒有成為下一個摩托羅拉或諾基亞。

    關於2014年第四季度,H T C在三季度財報說明中提到,“年度旗艦H T CO ne(M 8)與中端機型H T C D esire系列在競爭日趨激烈的智能手機市場保持穩定的銷售,市占率有所提升,延續 ...

  8. 一步步教你读懂NET中IL(附带图)

    一步步教你读懂NET中IL(附带图) 接触NET也有1年左右的时间了,NET的内部实现对我产生了很大的吸引力,在msdn上找到一篇关于NET的IL代码的图解说明,写的挺不错的.个人觉得:能对这些底部的 ...

  9. 一步步教你轻松学主成分分析PCA降维算法

    一步步教你轻松学主成分分析PCA降维算法 (白宁超 2018年10月22日10:14:18) 摘要:主成分分析(英语:Principal components analysis,PCA)是一种分析.简 ...

随机推荐

  1. cartographer 最新版安装测试

    在官网的基础上稍加修改,但保证代码都是最新的 我的系统配置: Debian9 strech,  ROS lunar 该方法对 ubuntu 系列操作系统以及其他ROS版本同样适用. 1.  安装依赖库 ...

  2. 论文笔记:Selective Search for Object Recognition

    与 Selective Search 初次见面是在著名的物体检测论文 「Rich feature hierarchies for accurate object detection and seman ...

  3. 【转】MySQL-Select语句高级应用

    [转]MySQL-Select语句高级应用 1.1 SELECT高级应用 1.1.1 前期准备工作 本次测试使用的是world数据库,由mysql官方提供下载地址: https://dev.mysql ...

  4. 在PHP中使用AES加密算法加密数据及解密数据

    这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原. 除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解. 在PHP中,我们必须先安装好mcr ...

  5. VC 为程序创建快捷方式的详细讲解

    有时候,为了方便用户使用我们编写的程序,需要在桌面,快速启动或程序组中创建程序的快捷方式.下面就介绍在VC下如何为程序创建快捷方式. 一.得到桌面,快速启动或程序组的路径这里介绍二个win32 API ...

  6. svn服务器镜像备份

    server master  192.168.0.100-->源版本库 server slave     192.168.0.101-->镜像版本库 1.初始化svnsync init s ...

  7. iOS post提交数据有嵌套数组的处理方法

    2017年11月21日17:11:43 解决办法, 修改iOS框架里的代码: http://www.jianshu.com/p/130daa0c2fe7 确实有效, 要不然,  内层的每一个键值对都会 ...

  8. 前端-----margin用法(盒子模型里补充)

    margin塌陷问题 当时说到了盒模型,盒模型包含着margin,为什么要在这里说margin呢?因为元素和元素在垂直方向上margin里面有坑. 我们来看一个例子: html结构: <div ...

  9. 37)django-单例模式

    一:单例模式 单例模式,是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类. 通过单例模式可以保证系统中一个类只有一个实例.即一个类只有一个对象实例. 常用例子:数据库连接串,只保 ...

  10. 28)django-form+ajax实现验证

    form+ajax实现验证 #obj.errors返回的是ErrorDict,不是字典(虽然继承字典) #obj.errors.as_json() 返回的字符串(前端要连续反解两次) #obj.err ...