步步為營-98-MyAPI
2021-04-22 20:27
阅读:524
标签:image 本地 width event 程序管理 add string prot 效果
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 IEnumerableStudents { get; set; } public static IEnumerable Teachers { get; set; } static Storages() { Students = new List () { new Student {Id=1,Name="逍遥小天狼1",Age=11,Gender=false}, new Student {Id=2,Name="逍遥小天狼2",Age=12,Gender=false}, new Student {Id=3,Name="逍遥小天狼3",Age=13,Gender=false}, new Student {Id=4,Name="逍遥小天狼4",Age=14,Gender=false}, new Student {Id=5,Name="逍遥小天狼5",Age=15,Gender=false}, new Student {Id=6,Name="逍遥小天狼6",Age=16,Gender=false}, }; Teachers = new List (); } } 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 { } }
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 { ////// 学生资源集合 /// public class StudentsController : ApiController { //C R U D public IEnumerable Get() { return Storages.Students; } public Student Get(string name) { return Storages.Students.FirstOrDefault(s=>s.Name.Equals(name,StringComparison.InvariantCultureIgnoreCase)); } } }
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}); } } }
运行效果
添加其他接口


using MyAPIN.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace MyAPIN.Controllers { ////// 学生资源集合 /// public class StudentsController : ApiController { //C R U D public IEnumerable 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 ; entity.Id = Storages.Students.Max(s=>s.Id)+1; list.Add(entity); } public void Delete([FromUri] string item) { var entity = Get(item); var list = Storages.Students as IList ; list.Remove(entity); } public void Put([FromUri] string item,[FromBody] Student entity) { Delete(item); Post(entity); } } }
4 客戶端調用
添加"控制台應用程序" 引用web Api 2.2 Client
步步為營-98-MyAPI
标签:image 本地 width event 程序管理 add string prot 效果
原文地址:http://www.cnblogs.com/YK2012/p/7997403.html
上一篇:C#要点补充
评论
亲,登录后才可以留言!