API services for both web & devices

Post on 04-Aug-2015

318 views 2 download

Tags:

Transcript of API services for both web & devices

API Servicesfor both web and

devicesDave Voyles

Technical Evangelist

Email: Dvoyles@Microsoft.com

@DaveVoyles

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

How ASP.NET Web API Fits In

ASP.NET Core

Web API

JSON XML

Web Forms

HTML

MVCWeb Pages

Introduction to ASP.NET Web API

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

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

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

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?

Manipulating HTTP Responses

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

Manipulating HTTP Responses

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

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

Microsoft /web

®

Posting Data to a Web API

DemoFile / New Project / Web API

RecapHow does ASP.NET Web API fit in?Understanding HTTP APIsIntroduction to Web APIConsuming Web APIs in web appsConsuming Web APIs in client apps

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