步步為營-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

 

posted @ 2017-12-09 10:50  逍遥小天狼  阅读(270)  评论(0编辑  收藏  举报