zoukankan      html  css  js  c++  java
  • 步步為營-98-MyAPI

    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=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<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)+1;
                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);
            }
        }
    }
    View Code

    4 客戶端調用

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

  • 相关阅读:
    体检前注意事项
    SSO之CAS单点登录详细搭建教程
    如何通过session控制单点登录
    谈谈防止Ajax重复点击提交
    js判断是移动端还是pc端
    HttpClient通过GET和POST获取网页内容
    HttpClient 4.x 执行网站登录并抓取网页的代码
    360每日自动签到,领取积分 (java httpclient4.x)
    Java @override报错的解决方法
    无开发经验,初学python
  • 原文地址:https://www.cnblogs.com/YK2012/p/7997403.html
Copyright © 2011-2022 走看看