ASP. NET CORE WEB API DEMO 01

Source: Internet
Author: User

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

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.