layout: true
--- class: center, middle # WebAPI Return codes => ( press > ) https://ignatandrei.github.io/Presentations/WebAPIReturnsprez.html Code at https://ignatandrei.github.io/Presentations/WebAPIReturns.zip --- # Agenda 0. About me 1. What is WebAPI ? 3. Simple Returns object 4. HttpCodes returns 3. Reply Returns 4. Links 7. Discussion / Questions ? --- class: center, top # About me ![:img ignat, 10%](ignat.jpg) Andrei Ignat http://msprogrammer.serviciipeweb.ro/ www.ASP.NET forum moderator YouTube 5 minutes .NET and tools : http://bit.ly/5MinTools Book Making Open Source Component : http://bit.ly/NetOpenSourceComponent Book Copy Paste from StackOverflow : https://amzn.to/2PQ8EDc Roslyn Source Code Generators with examples : Amazon: https://amzn.to/3f6gll3 Free: https://ignatandrei.github.io/RSCG_Examples/ Monthly meetings: https://www.meetup.com/Bucharest-A-D-C-E-S-Meetup/ --- class: center, top # What is WebAPI ? Simple method to exchange data and commands via http can be used to CRUD over database ( not recommended ) Demo: WebAPI project --- class: top # Simple Returns object Demo Get ( success, error , null ) Demo Post with Validation ```csharp [HttpGet("{id}")] public Person GetPerson(int id) { return RetrieveFromDatabase(id); } [HttpPost] public int SavePerson(Person p) { // save to database, then return p.ID; } ``` --- class: top # HttpCodes returns ```csharp [HttpGet("{id}")] public ActionResult
GetPerson404(int id) { var p = RetrieveFromDatabase(id); if (p == null) return NotFound($"{nameof(Person)} with {id} are not found"); return p; } ``` --- class: top # Reply Returns ```csharp [HttpGet("{id}")] public ReplyData
GetWithReply(int id) { return RetrieveWithReplyFromDatabase(id); } private ReplyData
RetrieveWithReplyFromDatabase(int id) { try { Person p = RetrieveFromDatabase(id); if (p == null) { var r = new ReplyData
(); r.Success = false; r.Message = "Cannot find person with id " + id; return r; } else { var r = new ReplyData
(); r.Success = true; r.ReturnObject = p; return r; } } catch (Exception ex) { var r = new ReplyData
(); r.Success = false; r.Message = ex.Message; return r; } } ``` --- class: center, top # Links to get started https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio https://restfulapi.net/richardson-maturity-model https://blog.ploeh.dk/2013/05/01/rest-lesson-learned-avoid-hackable-urls/ --- class: left, top # Questions? Andrei Ignat http://msprogrammer.serviciipeweb.ro/ www.ASP.NET forum moderator YouTube 5 minutes .NET and tools : http://bit.ly/5MinTools Book Making Open Source Component : http://bit.ly/NetOpenSourceComponent Book Copy Paste from StackOverflow : https://amzn.to/2PQ8EDc Roslyn Source Code Generators with examples : Amazon: https://amzn.to/3f6gll3 Free: https://ignatandrei.github.io/RSCG_Examples/ Monthly meetings: https://www.meetup.com/Bucharest-A-D-C-E-S-Meetup/