首頁  >  文章  >  後端開發  >  詳細介紹C# 中 ASP.NET Web API 的 ROC

詳細介紹C# 中 ASP.NET Web API 的 ROC

php是最好的语言
php是最好的语言原創
2018-07-27 10:36:331935瀏覽

web api 是一個 面向資源(ROC)  透過 HTTP協定動詞來修改資源狀態的可自我寄宿(SelfHost)的接口 今天seaconch 的主要目的是實作一個簡單的ASP.NET Web API 栗子

什麼是Web API

談談REST與ASP.NET Web API

怎麼理解REST 、RESTful

關於什麼是Web API seaconch 也就不再這裡多說了

web api 是一個面向資源(ROC)  透過HTTP協定動詞來修改資源狀態的可自我寄宿( SelfHost)的介面

今天seaconch 的主要目的是實作一個簡單的ASP.NET Web API 栗子

新建一個ASP.NET Web API 專案

#1.新項目

詳細介紹C# 中 ASP.NET Web API 的 ROC

2.選擇Web API

詳細介紹C# 中 ASP.NET Web API 的 ROC

#3.新Person 類別

詳細介紹C# 中 ASP.NET Web API 的 ROC

#Person類別:

    /// <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.新Person_Context 類別

Person_Context 類別:

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);
        }
    }
}

5.設定連接字串

詳細介紹C# 中 ASP.NET Web API 的 ROC

6.設定EF 初始資料

詳細介紹C# 中 ASP.NET Web API 的 ROC

#對Person 的Get 請求

1.新建Controller

右鍵Controllers 新建一個空的API

詳細介紹C# 中 ASP.NET Web API 的 ROC

#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.呼叫API

這裡我們用小程式來測試效果
詳細介紹C# 中 ASP.NET Web API 的 ROC

對Person 的POST 請求

1.Post

這裡我們在Post API 中,新增了一個新的Person -> 布布

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

2.小程式代碼

相信有心的同學已經發現了,小程式中我們只修改了method 由GET -> ;POST

對Person 的PUT 請求

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

2.小程式碼

詳細介紹C# 中 ASP.NET Web API 的 ROC

結尾

至此,一個簡單的對Person 進行HTTP GET / POST / PUT 操作的ASP.NET Web API 已經呈現在了大家面前

過程中我們也可以看到,為什麼說ASP .NET Web API 是ROC ?你也可以看到對於微信小程式而言,她僅僅是修改了請求method,那麼就實現了對Person 這個資源的不同操作

然而我們並沒有開放對於Person 資源的DELETE 請求方式,那麼對應的我們就等於沒有對外開放Person 的刪除方式

好了,今天就到這啦,相信大家對於ASP.NET Web API 也有了一個初步的認知

相關文章:

C boost::asio程式設計-網域解析詳細介紹

#正規在C 中使用的詳細介紹

相關影片:

C# 教學

#

以上是詳細介紹C# 中 ASP.NET Web API 的 ROC的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn