WebAPI, webapipost
WebAPI is completely based on the RESTful standard (HTTP protocol), WCF and WebService (SOAP Protocol)
So what is RESTful?
RESTful is resource-oriented. An address represents a resource.
Http://www.demo.com/student can represent the resources of Students
Http://www.demo.com/student/zhang San can represent a resource called Zhang San.
WebAPI is a framework that implements the concept of RESTful. It establishes a framework that can be called by various clients (such as browsers, smart phone terminals, and desktop clients) for HTTP Services. It is not part of the ASP. net mvc framework. It can be used as part of the ASP. NET platform for MVC, Web Form, or as an independent service.
Next we will build a simple webAPI
Create an empty web Application
Download webAPI package with NuGet
Create an object class as the data source
1 using System; 2 using System. collections; 3 using System. collections. generic; 4 using System. linq; 5 using System. web; 6 namespace WebAPI. models 7 {8 public class Students 9 {10 public int Id {get; set;} 11 public string Name {get; set;} 12 public int Age {get; set ;} 13 public static IList <Students> StudentsStorage = new List <Students> 14 {15 new Students {Id = 1, Name = "James", Age = 20 }, 16 new Students {Id = 2, Name = "", Age = 21}, 17 new Students {Id = 3, Name = "", Age = 22 }, 18}; 19} 20}
Create a StudentsController class so that it inherits ApiController
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Http; 7 using WebAPI.Models; 8 namespace WebAPI.Cotntrollers 9 {10 public class StudentsController:ApiController11 {12 //Get:/Students13 public IEnumerable<Students> Get()14 {15 return Students.StudentsStorage;16 }17 //Get:/Students/218 public Students Get(int item)19 {20 return Students.StudentsStorage.FirstOrDefault(s => s.Id.Equals(item));21 }22 //POST:/Students23 public void Post(Students model)24 {25 Students.StudentsStorage.Add(model);26 }27 //Put:/Students28 public void Put(int item, Students model)29 {30 var exitst = Students.StudentsStorage.FirstOrDefault(s => s.Id.Equals(item));31 if (exitst != null)32 {33 model.Id = exitst.Id;34 35 }36 Students.StudentsStorage.Remove(exitst);37 Students.StudentsStorage.Add(model);38 }39 //DElETE:student/140 public void Delete(int item)41 {42 var exitst = Students.StudentsStorage.FirstOrDefault(s => s.Id.Equals(item));43 Students.StudentsStorage.Remove(exitst);44 }45 }46 }
Create a Global. asax instance to configure a WebAPI route
1 protected void Application_Start (object sender, EventArgs e) 2 {3 // WebAPI Route 4 GlobalConfiguration. configuration. routes. mapHttpRoute (5 "default_api", 6 "{controller}/{item}", 7 defaults: new {item = RouteParameter. optional}); 8}
Well, a simple WebAPI is ready. Let's test this WebAPI.
Why is it XML? Let's take a look.
From this we can see that the Content-Type we sent is appliction/xml
What should we do if we need data in Json format? In fact, we can get the data in json format by changing the Content-Type of the request to appliction/json.
We can also use this tool to test the WebAPI get post put delete method we just wrote.