api services for both web & devices

15
API Services for both web and devices Dave Voyles Technical Evangelist Email: [email protected] @DaveVoyles

Upload: david-voyles

Post on 04-Aug-2015

323 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: API services for both web & devices

API Servicesfor both web and

devicesDave Voyles

Technical Evangelist

Email: [email protected]

@DaveVoyles

Page 2: API services for both web & devices

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

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

Introduction to ASP.NET Web API

Page 5: API services for both web & devices

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

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

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

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

Manipulating HTTP Responses

A successful API call returns an HTTP OK and the JSON data

Page 10: API services for both web & devices

Manipulating HTTP Responses

An unsuccessful API call returns an HTTP 404 (and no JSON)

Page 11: API services for both web & devices

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

Microsoft /web

®

Posting Data to a Web API

Page 13: API services for both web & devices

DemoFile / New Project / Web API

Page 14: API services for both web & devices

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

© 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.