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. CentOS7 设置主机名及IP映射

    1.设置主机名 查看本机的主机名,使用如下三个命令中任意一个即可 # hostname # uname -n # cat /proc/sys/kernel/hostname 使用 vi 编辑器打开 / ...

  2. python,random随机数的获取

    随机数生成 首先我们需要在程序中引入random>>>import random as r r.random()用于生成一个随机的浮点数, >>> print(r. ...

  3. 第一节,tensorflow基础构架

    1.tensorflow结构 import tensorflow as tfimport numpy as np #create datax_data=np.random.rand(100).asty ...

  4. ASP.NET MVC 4 从示例代码展开,连接默认SQL Server数据库

    VS2013里面,点击菜单[视图]-[SQL server对象资源管理器],右键点击[SQL Server]节点,选择[添加SQL Server]自动生成. 这只是开始,可以让网上下载下来的例子运行出 ...

  5. 快速搭建ELK日志分析系统

    一.ELK搭建篇 官网地址:https://www.elastic.co/cn/ 官网权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/ ...

  6. [转]python3之模块psutil系统性能信息

    转自:https://www.cnblogs.com/zhangxinqi/p/9106265.html 阅读目录 1.psutil模块安装 2.获取CPU信息 3.内存信息 4.磁盘信息 5.网络信 ...

  7. Delphi XE-Windows下配置开发环境 (Android版/IOS)

    Delphi XE-Windows下配置开发环境  (Android版/IOS)   http://www.52jike.com/thread-1-1-1.html Delphi XE5的Androi ...

  8. 【Boost】boost::tokenizer详解

    分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报   目录(?)[+]   tokenizer 库提供预定义好的四个分词对象, 其中char ...

  9. Unity中的GC以及优化

    [简介] 常见的 Unity GC 知识点总结出来的思维导图 Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译 ...

  10. ebs 12.1.1打中文补丁

    环境变量设置 oracle 用户,应用env环境变量 . /u01/app/db/tech_st/11.1.0/PROD_erpapp1.env alias s='sqlplus / as sysdb ...