Web api is a resource-oriented (ROC) self-hosting (SelfHost) interface that modifies resource status through HTTP protocol verbs The main purpose of seaconch today is to implement a simple ASP.NET Web API. Chestnut
Talk about REST and ASP.NET Web API
How to understand REST , RESTful
I won’t say much more about what Web API seaconch is here
web api is a resource-oriented (ROC) self-hosting that modifies resource status through HTTP protocol verbs ( SelfHost) interface
The main purpose of seaconch today is to implement a simple ASP.NET Web API chestnut
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; } }
Person_Context class:
using System.Collections.Generic; namespace chestnut_webapi.Models { public class Person_Context : System.Data.Entity.DbContext { public Person_Context() : base("name=sc_db") { } public System.Data.Entity.DbSet<Person> Persons { get; set; } protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { modelBuilder.Conventions.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 = "毛毛", Age = 13, Sex = 1 }); context.Persons.Add(new Person() { Name = "团团", Age = 12, Sex = 2 }); base.Seed(context); } } }
Right-click Controllers to create an empty API
public class PersonController : ApiController { Models.Person_Context person_db = new Models.Person_Context(); public List<Models.Person> Get() { return person_db.Persons.ToList(); } }
Here we use a small program to test the effect
Here we are in the Post API , added a new Person -> Bubu
public List<Models.Person> Post() { Models.Person p = new Models.Person() { ID = 1, Name = "布布", Age = 5, Sex = 1 }; person_db.Persons.Add(p); person_db.SaveChanges(); return person_db.Persons.ToList(); }
I believe interested students have discovered it. In the mini program, we only modified the method by GET -> ; POST
public List<Models.Person> Put() { Models.Person person = person_db.Persons.Where(p => p.Name == "团团").ToList().Single(); person.Name = "圆圆"; person_db.SaveChanges(); return person_db.Persons.ToList(); }
At this point, a simple ASP.NET Web API that performs HTTP GET / POST / PUT operations on Person has been presented to everyone
We can also see in the process why it is said that ASP .NET Web API is ROC? You can also see that for the WeChat applet, she only modified the request method, and then realized different operations on the Person resource.
However, we did not open the DELETE request method for the Person resource. So correspondingly, we have not opened the method of deleting Person to the outside world
Okay, that’s it for today. I believe everyone has a preliminary understanding of ASP.NET Web API
Related Article:
C boost::asio programming-detailed introduction to domain name resolution
Detailed introduction to the use of regular expressions in C
Related videos:
The above is the detailed content of Detailed introduction to the ROC of ASP.NET Web API in C#. For more information, please follow other related articles on the PHP Chinese website!