The Web API is a resource-oriented (ROC) interface that modifies the state of the resource through the HTTP protocol verb to self-host (Selfhost) interfaces today Seaconch's main purpose is to implement a simple ASP. NET Web API Chestnut
What is a web API
Talk about the rest and ASP. NET Web API
How to understand REST, restful
about what is the Web API seaconch no more talking here.
The Web API is a self-hosted (Selfhost) interface for resource-oriented (ROC) modification of resource state via HTTP protocol verbs
The main purpose of today's seaconch is to implement a simple ASP. NET Web API Chestnut
Create a new ASP. NET Web API Project
1. New Project
2. Select the Web API
3. New Person class
Person class:
<summary>//// </summary> public class Person {public int ID {get; set;} public string Name {get; set;} public int Sex {get; set;} public int Age {get; set;} }
4. New Person_context class
Person_context class:
Using System.collections.generic;namespace Chestnut_webapi. models{public class Person_Context:System.Data.Entity.DbContext {public Person_context (): BAS E ("name=sc_db") {} public system.data.entity.dbset<person> Persons {get; set;} protected override void Onmodelcreating (System.Data.Entity.DbModelBuilder modelBuilder) {modelbuilder.c Onventions. Remove<system.data.entity.modelconfiguration.conventions.pluralizingtablenameconvention> (); }} public class Db_initer:system.data.entity.dropcreatedatabasealways<person_context> {protected override void Seed (Person_context context) {context. Persons.add (new person () {Name = "fluffy", age = +, Sex = 1 }); Context. Persons.add (new person () {Name = "round and round", age = 2, Sex = }); Base. Seed (context); } }}
5. Configure the connection string
6. Configure EF Initial data
Get Request for Person
1. New Controller
Right-Controllers Create a new empty API
2.GET:
public class Personcontroller:apicontroller { Models.person_context person_db = new Models.person_context (); Public list<models.person> Get () { return person_db. Persons.tolist (); } }
3. Calling the API
Here we use a small program to test the effect
POST Request to Person
1.Post
Here we have added a new person-to-boo in the Post API
Public list<models.person> Post () { Models.person p = new Models.person () { ID = 1, Name = "cloth ", Age = 5, Sex = 1 }; person_db. Persons.add (p); person_db. SaveChanges (); Return person_db. Persons.tolist (); }
2. Small program code
Believe that the heart of the classmate has found that in the small program we only modified the method by GET, POST
PUT Request for Person
1.PUT
Public list<models.person> Put () { Models.person person = person_db. Persons.where (p = = P.name = = "Tuan"). ToList (). Single (); Person. Name = "Round"; person_db. SaveChanges (); Return person_db. Persons.tolist (); }
2. Small program code
End
At this point, a simple ASP. HTTP Get/post/put operation of the person is already present in front of everyone
We can also see in the process why the ASP. NET Web API is ROC? You can also see that for small programs, she just modifies the request method, and then implements a different operation on the person resource.
However, we do not open the delete request for the person resource, then we will be equal to the deletion of the person to open the way
OK, here we are today, I believe you have a preliminary understanding of the ASP. NET Web API
Related articles:
C + + Boost::asio programming-domain name resolution detailed introduction
A detailed introduction to regular use in C + +
Related videos:
C # Tutorials