Web API a RESTful architecture-style Web service. The so-called rest architecture has nothing to do with technology, but rather a resource-oriented software architecture design.
WCF has also provided support for restful styles since 3.5, but rather cumbersome compared to WEBAPI, WEBAPI provides a more lightweight communication architecture.
Let's see how to create a WEBAPI service
Start by creating a new solution, and create a new WEBAPI Project under the solution.
In the newly created WEBAPI project, the new controller (similar to the creation of MVC), we are named Customercontroller, in Customercontroller we provide the customer data
Simple Add and query functionality. The specific need to call warehousing to achieve internal logic processing, warehousing class code as follows
Model class
Namespace customerservice.models{public class Customer { int id { get; set; } public string name { get; SET;  public string email { get; SET; }    }
namespace customerservice.repository{ Public interface icustomerrepository { Customer getcust Omer (int Add (customer customer); }}
Namespace customerservice.repository{PublicClassCustomerrepository:icustomerrepository {PrivateStaticlist<Customer> customers =Newlist<Customer> () {NewCustomer () {ID = 1, Name ="Zhangsan", Email ="Zhangsan@dx.com"},NewCustomer () {ID = 2, Name ="Lisi", Email ="Lisi@dx.com"},NewCustomer () {ID = 3, Name ="Wangwu", Email ="Wangwu@dx.com"};PublicCustomer GetCustomer (int id) {Return customers. FirstOrDefault (c = c.id = = ID); }Publicint Add (Customer customer) {int maxid = 0;if (Customers != null) { maxid = customers. Max (c => c.id); customer.ID = maxId; customers. ADD (Customer); } return maxId; } }}
Here's a look at the controller's code.
namespacecustomerservice.controllers{ Public classCustomercontroller:apicontroller {Privateicustomerrepository customerrepository; PublicCustomercontroller () {customerrepository=Newcustomerrepository (); } PublicCustomer Getcustomerbyid (intID) {returnCustomerrepository.getcustomer (ID); } Public intAddcustomer (Customer customer) {returnCustomerrepository.add (customer); } }}
These are the steps to create a Web API service. There is also a routing configuration in Webapi that can personalize the address of the service. Specifically, the App_start file that is automatically generated in the project is added to the configuration in WebApiConfig.cs, the code is as follows
namespacecustomerservice{ Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {config. Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"GetCustomer", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); Config. Routes.maphttproute (Name:"Addcustomer", Routetemplate:"Api/{controller}/add", defaults:New{id =routeparameter.optional}); } }}
The above code is called in the Application_Start () method (automatically generated when a new WEBAPI project is created)
namespace customerservice{ publicclass WebApiApplication:System.Web.HttpApplication { protectedvoid Application_Start () { Globalconfiguration.configure (webapiconfig.register); }}}
At this point, our first WEBAPI service has been created successfully, after starting running the project (service started state), enter the address of the service in the browser (chrom) to query a single customer
HTTP://LOCALHOST:24434/API/CUSTOMER/1 browser will show the results in XML format
ASP. NET Webapi Service creation