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. oracle12.2.1 坑

    1.ORA-28040: No matching authentication protocol 解决:在Oracle用户(不是grid用户)下,将$ORACLE_HOME/network/admin ...

  2. xadmin后台页面定制和添加服务器监控组件

    xadmin定制 项目需要添加服务器监控页面,碍于xadmin不是很好自定义页面,之前写过插件,太麻烦了,还是直接改源码 原理其实很简单,因为xadmin的处理流程和django类似,都是通过拦截UR ...

  3. qtcreator 快捷键常用

    F1        查看帮助F2        跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2    声明和定义之间切换F4        头文件和源文件之间切换Ctrl+1     ...

  4. [sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值

    官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...

  5. Python学习-环境搭建(IronPython)

    一.IDE环境 VS2013  下安装 PTVS 2.2.2 VS 2013 VS2015 已经集成了Python的开发环境 二.安装 IronPython 下载地址:http://ironpytho ...

  6. input[type=file]上传文件(格式判断、文件大小、上传成功后操作)

    var isUploadImg = false; //在input file内容改变的时候触发事件******************上传图片 $('#filed').change(function( ...

  7. VC常用小知识

    (1) 如何通过代码获得应用程序主窗口的 指针?主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现.AfxGetMainWnd() ->Sho ...

  8. 【转】浅谈Java中的hashcode方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...

  9. webpack配置的基本介绍

    https://github.com/DDFE/DDFE-blog/issues/10 全局安装 webpack :(当前笔记版本: webpack  3.10.0 , mac环境) 1. npm i ...

  10. MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解

    MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解 Percona XtraDB Cluster简称PXC.Percona Xtradb Cluster的实现是在 ...