Web API は、HTTP プロトコル動詞を通じてリソースのステータスを変更するリソース指向 (ROC) セルフホスティング (SelfHost) インターフェイスです。 今日の seaconch の主な目的は、単純な ASP.NET Web API を実装することです。Chestnut
REST と ASP.NET Web API について話す
方法REST 、 RESTful を理解するには
#ここでは Web API seaconch が何であるかについてはこれ以上は言いませんWeb API は、HTTP を通じてリソースのステータスを変更するリソース指向 (ROC) セルフホスティングです。プロトコル動詞 (SelfHost) インターフェイス今日の seaconch の主な目的は、単純な ASP.NET Web API を実装することです新しい ASP.NET Web API プロジェクトを作成します 1. 新しいプロジェクトを作成します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; } }
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); } } }
##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(); } }
POST リクエストを Person1 に送信します。 Post
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(); }
興味のある学生はそれを発見したと思います。ミニ プログラムでは、 GET -> ; POST
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(); }
##この時点で、人に対して HTTP GET / POST / PUT 操作を実行する単純な ASP.NET Web API が全員に提示されました
C
C# チュートリアル以上がC# での ASP.NET Web API の ROC の詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。