Detailed introduction to the ROC of ASP.NET Web API in C#

php是最好的语言
Release: 2018-07-27 10:36:33
Original
1972 people have browsed it

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

What is Web API

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

Create a new ASP.NET Web API project

1. Create a new project

Detailed introduction to the ROC of ASP.NET Web API in C#

2. Select Web API

Detailed introduction to the ROC of ASP.NET Web API in C#

3. Create a new Person class

Detailed introduction to the ROC of ASP.NET Web API in C#

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; }
    }
Copy after login

4. Create a 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()
            : 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);
        }
    }
}
Copy after login

5. Configure connection string

Detailed introduction to the ROC of ASP.NET Web API in C#

6. Configure EF initial data

Detailed introduction to the ROC of ASP.NET Web API in C#

Get request for Person

1. Create a new Controller

Right-click Controllers to create an empty API

Detailed introduction to the ROC of ASP.NET Web API in C#

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();
        }
    }
Copy after login

3.Call API

Here we use a small program to test the effect
Detailed introduction to the ROC of ASP.NET Web API in C#

POST request to Person

1.Post

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();
        }
Copy after login

2. Mini program code

I believe interested students have discovered it. In the mini program, we only modified the method by GET -> ; POST

PUT request to Person

1.PUT

        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();
        }
Copy after login

2. Mini program code

Detailed introduction to the ROC of ASP.NET Web API in C#

End

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:

C# Tutorial

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!