More information about the ROC of the ASP. NET Web API in C #

Source: Internet
Author: User
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

Related Article

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.