API services for both web & devices
-
Upload
david-voyles -
Category
Technology
-
view
318 -
download
2
Transcript of API services for both web & devices
![Page 1: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/1.jpg)
API Servicesfor both web and
devicesDave Voyles
Technical Evangelist
Email: [email protected]
@DaveVoyles
![Page 2: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/2.jpg)
Agenda
1)How does ASP.NET Web API fit in?2)Understanding HTTP APIs3) Introduction to Web API4)Consuming Web APIs in web apps5)Consuming Web APIs in client apps
![Page 3: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/3.jpg)
How ASP.NET Web API Fits In
ASP.NET Core
Web API
JSON XML
Web Forms
HTML
MVCWeb Pages
![Page 4: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/4.jpg)
Introduction to ASP.NET Web API
![Page 5: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/5.jpg)
Microsoft /web
®
Sample Read-only Model and Controllerpublic class Person{ public int Id { get; set; } public string Name { get; set; }}
Step 1:Create a Model
public class PersonController : ApiController{ List<Person> _people; public PersonController() { _people = new List<Person>(); _people.AddRange(new Person[] { new Person { Id = 1, Name = "Chuck Norris" }, new Person { Id = 2, Name = "David Carradine" }, new Person { Id = 3, Name = "Bruce Lee" } }); }}
Step 2:Make an API Controller
![Page 6: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/6.jpg)
Microsoft /web
®
Read-only Controller Actions to return data// GET /api/personpublic IEnumerable<Person> Get(){ return _people;}
Step 3:Return everything
// GET /api/person/5public Person Get(int id){ return _people.First(x => x.Id == id);}
Step 4:Return one item
![Page 7: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/7.jpg)
Microsoft /web
®
Routing a Web API
public static void RegisterRoutes(RouteCollection routes){ routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );}
Routing:Familiar syntax, conventional approach
![Page 8: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/8.jpg)
Manipulating HTTP Responses
// GET /api/person/5public HttpResponseMessage<Person> Get(int id){ try { var person = _people.First(x => x.Id == id);
return new HttpResponseMessage<Person>( person, HttpStatusCode.OK ); } catch { return new HttpResponseMessage<Person>(HttpStatusCode.NotFound); }}
ExampleFind a person and return it,but what happens if we don’t find a match?
![Page 9: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/9.jpg)
Manipulating HTTP Responses
A successful API call returns an HTTP OK and the JSON data
![Page 10: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/10.jpg)
Manipulating HTTP Responses
An unsuccessful API call returns an HTTP 404 (and no JSON)
![Page 11: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/11.jpg)
Microsoft /web
®
Posting Data to a Web APIpublic HttpResponseMessage Post(Person person){ person.Id = _people.Count + 1;
if (_people.Any(x => x.Id == person.Id)) return new HttpResponseMessage(HttpStatusCode.BadRequest);
try { _people.Add(person); } catch { return new HttpResponseMessage(HttpStatusCode.BadRequest); }
return new HttpResponseMessage(HttpStatusCode.OK);}
Use HTTP Post:Pass a Model
![Page 12: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/12.jpg)
Microsoft /web
®
Posting Data to a Web API
![Page 13: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/13.jpg)
DemoFile / New Project / Web API
![Page 14: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/14.jpg)
RecapHow does ASP.NET Web API fit in?Understanding HTTP APIsIntroduction to Web APIConsuming Web APIs in web appsConsuming Web APIs in client apps
![Page 15: API services for both web & devices](https://reader030.fdocuments.in/reader030/viewer/2022032503/55bfc83ebb61eb7f4e8b4720/html5/thumbnails/15.jpg)
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.