步步為營-98-MyAPI

时间:2021-06-13 12:51:30

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

运行效果

步步為營-98-MyAPI

添加其他接口

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

步步為營-98-MyAPI