usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;namespacedemo.models{ Public classBookchapter { PublicGuid Id {Get;Set; } Public intNumber {Get;Set; } Public stringTitle {Get;Set; } Public intPages {Get;Set; } } Public InterfaceIbookchaptersrepository {voidInit (); IEnumerable<BookChapter>GetAll (); Bookchapter Find (Guid ID); voidADD (Bookchapter chapter); Bookchapter Remove (Guid ID); voidUpdate (Bookchapter chapter); } Public classBookchaptersrepository:ibookchaptersrepository {Private ReadOnlyConcurrentdictionary<guid, bookchapter> _chapters =NewConcurrentdictionary<guid, bookchapter>(); Public voidInit () {ADD (Newbookchapter {Number =1, Title ="Application Architectures", Pages = * }); ADD (Newbookchapter {Number =2, Title ="Core C #", Pages = the }); } PublicIenumerable<bookchapter> GetAll () =_chapters. Values; Publicbookchapter Find (Guid ID) {Bookchapter chapter; _chapters. TryGetValue (ID, outchapter); returnChapter; } Public voidADD (Bookchapter Chapter) {Chapter. Id=Guid.NewGuid (); _chapters[chapter. ID]=Chapter; } Publicbookchapter Remove (Guid ID) {Bookchapter chapter; _chapters. Tryremove (ID, outchapter); returnChapter; } Public voidUpdate (Bookchapter chapter) {_chapters[chapter. ID]=Chapter; } }}
usingSystem;usingSystem.Collections.Generic;usingDemo.models;usingMICROSOFT.ASPNETCORE.MVC;namespacedemo.controllers{[Produces ("Application/json", ("Application/xml")] [Route ("Api/[controller]")] Public classBookchapterscontroller:controller {Private ReadOnlyibookchaptersrepository _repository; PublicBookchapterscontroller (ibookchaptersrepository bookchaptersrepository) {_repository=bookchaptersrepository; } //GET api/bookchapters[HttpGet] PublicIenumerable<bookchapter> getbookchapters () =_repository. GetAll (); //GET Api/bookchapters/guid[HttpGet ("{ID}", Name =nameof (Getbookchapterbyid))] PublicIactionresult Getbookchapterbyid (Guid ID) {Bookchapter chapter=_repository. Find (ID); if(Chapter = =NULL) { returnNotFound (); } return NewObjectresult (chapter); } //POST api/bookchapters[HttpPost] Publiciactionresult Postbookchapter ([Frombody]bookchapter chapter) {if(Chapter = =NULL) { returnbadrequest (); } _repository. ADD (chapter); returnCreatedatroute (nameof (Getbookchapterbyid),New{id =Chapter. ID}, chapter); } //PUT api/bookchapters[Httpput ("{ID}")] PublicIactionresult putbookchapter (Guid ID, [Frombody]bookchapter chapter) {if(Chapter = =NULL|| ID! =Chapter. Id) {returnbadrequest (); } if(_repository. Find (id) = =NULL) { returnNotFound (); } _repository. Update (chapter); return NewNocontentresult (); } //DELETE api/bookchapters/id[Httpdelete ("ID")] Public voidDelete (Guid id) {_repository. Remove (ID); } }}
usingDemo.models;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.DependencyInjection;namespacedemo{ Public classStartup { PublicStartup (iconfiguration configuration) {Configuration=configuration; } PublicIConfiguration Configuration {Get; } Public voidconfigureservices (iservicecollection services) {services. Addmvc (). Addxmlserializerformatters (); Ibookchaptersrepository Repos=Newbookchaptersrepository (); Repos. Init (); Services. Addsingleton (repos); } Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } app. Usemvc (); } }}
ASP. NET CORE WEB API DEMO 01