1 adding a reference to Microsoft ASP. WebAPI 2.2 Through the NuGet Program Management Pack
2 Add two folders controllers and models
2.1 Impersonate the database locally, so add the Storages class in the Models folder
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacemyapin.models{ Public Static classstorages { Public StaticIenumerable<student> Students {Get;Set; } Public StaticIenumerable<teacher> Teachers {Get;Set; } Staticstorages () {Students=NewList<student>() { NewStudent {id=1, name="Carefree Little Day Wolf 1", age= One, gender=false}, NewStudent {id=2, name="Carefree Little Day Wolf 2", age= A, gender=false}, NewStudent {id=3, name="Carefree Little Day Wolf 3", age= -, gender=false}, NewStudent {id=4, name="Carefree Little Day Wolf 4", age= -, gender=false}, NewStudent {id=5, name="Carefree Little Day Wolf 5", age= the, gender=false}, NewStudent {id=6, name="Carefree Little Day Wolf 6", age= -, gender=false}, }; Teachers=NewList<teacher>(); } } Public classPerson { Public intId {Get;Set; } Public stringName {Get;Set; } Public intAge {Get;Set; } Public BOOLGender {Get;Set; } } Public classStudent:person {} Public classTeacher:person {}}
storages
2.2 Adding Studentscontroller and Teachercontroller in controllers
usingMyapin.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;namespacemyapin.controllers{/// <summary> ///Student Resource Collection/// </summary> Public classStudentscontroller:apicontroller {//C R U D PublicIenumerable<student>Get () {returnstorages.students; } PublicStudent Get (stringname) { returnStorages.Students.FirstOrDefault (s=>s.name.equals (name,stringcomparison.invariantcultureignorecase)); } }}
Studentscontroller
3 Adding a global portal file to configure API routing
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;usingSystem.Web.Security;usingSystem.Web.SessionState;namespacemyapin{ Public classGlobal:System.Web.HttpApplication {protected voidApplication_Start (Objectsender, EventArgs e) { //Configure API RoutingGlobalConfiguration.Configuration.Routes.MapHttpRoute ("Default_api", "{Controller}/{item}", New{item=routeparameter.optional}); } }}
Global
Run effect
Add additional interfaces
usingMyapin.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Http;namespacemyapin.controllers{/// <summary> ///Student Resource Collection/// </summary> Public classStudentscontroller:apicontroller {//C R U D PublicIenumerable<student>Get () {returnstorages.students; } PublicStudent Get (stringItem) { returnStorages.Students.FirstOrDefault (s=>s.name.equals (item,stringcomparison.invariantcultureignorecase)); } Public voidPost (Student entity) {varList = Storages.students asIlist<student>; Entity. Id= Storages.Students.Max (s=>s.id) +1; List. ADD (entity); } Public voidDelete ([Fromuri]stringItem) { varentity =Get (item); varList = Storages.students asIlist<student>; List. Remove (entity); } Public voidPut ([Fromuri]stringItem,[frombody] Student entity) {Delete (item); Post (entity); } }}
View Code
4 Customer-use
Add a "Console Application" reference Web API 2.2 Client
Step by step for Camp -98-myapi