Step by step for Camp -98-myapi

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.