excellent rest met de web api

33

Upload: maurice-beijer

Post on 18-Dec-2014

2.327 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Excellent rest met de web api
Page 2: Excellent rest met de web api

Excellent REST met de WebAPI

Maurice de Beijer

Page 3: Excellent rest met de web api

Wie ben ik

• Maurice de Beijer• The Problem Solver• Microsoft Integration MVP• DevelopMentor instructor• Twitter: @mauricedb• Blog: http://msmvps.com/blogs/theproblemsolver/

• Web: http://www.HTML5Support.nl• E-mail: [email protected]

Page 4: Excellent rest met de web api

Agenda• Wat is REST?• Wat is de ASP.NET WebAPI• Hypermedia

Page 5: Excellent rest met de web api

Wat is REST?

REpresentational State Transfer (REST) is een software-architectuur

voor gedistribueerde mediasystemen zoals het World wide web.

Wikipedia

Page 6: Excellent rest met de web api

Wat is REST?• Bedacht door Roy Thomas Fielding– Onderdeel van zijn doctoraalstudie

uit 2000– Een van de drie auteurs van de

Hypertext Transfer Protocol -- HTTP/1.0

• Een manier om web services te maken– Op basis van de HTTP standaard

Page 7: Excellent rest met de web api

Hypertext Transfer ProtocolThe Hypertext Transfer Protocol (HTTP) is

an application-level protocol for distributed, collaborative,

hypermedia information systems. It is a generic, stateless, protocol which

can be used for many tasks beyond its use for hypertext.

The Internet Engineering Task Force

Page 8: Excellent rest met de web api

Richardson Maturity Model

Page 9: Excellent rest met de web api

ASP.NET WebAPIASP.NET Web API is een framework dat het makkelijk maakt om HTTP

en REST services te bouwen op het .NET framework.

Page 10: Excellent rest met de web api

WebAPI Controllers• In een ApiController gebeurt het

werk– Toegang tot het Request en

Response

• ModelBinding maakt het werken met binnenkomende data gemakkelijk– Kan ook met een

HttpRequestMessage werken

Page 11: Excellent rest met de web api

WebAPI Controllers• Veel controle over data naar de

client– HttpResponseMessage– Content negotiation

• Kan ook HTTP headers zetten– Caching– Optimistic concurrency

Page 12: Excellent rest met de web api

WebAPI Controllerspublic class DemoController : ApiController{ // GET api/demo public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }}

Page 13: Excellent rest met de web api

WebAPI Routes• Routes koppelen URL aan

Controller– Net als in ASP.NET MVC

• Je kan er meerdere hebben– De volgorde is van belang

Page 14: Excellent rest met de web api

WebAPI Routespublic static void Register(HttpConfiguration config){ config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );}

Page 15: Excellent rest met de web api

demoWebAPI

Page 16: Excellent rest met de web api

Content negotiation• Wat we versturen != hoe we het

weergeven– JSON of XML: een boek blijft een

boek

Page 17: Excellent rest met de web api

MediaTypeFormatter• Een media type geeft het formaat

aan– JSON, XML, Word, PDF, VCard etc

• Een MediaTypeFormatter converteert– HTTP <> CLR type

• Content negotiation bepaalt het type– De client gebruikt de Accept header

Page 18: Excellent rest met de web api

MediaTypeFormatterpublic class CustomersTextFormatter : BufferedMediaTypeFormatter{ public CustomersTextFormatter() { SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/text")); }

public override bool CanWriteType(Type type) { return typeof(IEnumerable<Customer>).IsAssignableFrom(type); }

public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content) { // ... }}

Page 19: Excellent rest met de web api

demoContent Negotiation

Page 20: Excellent rest met de web api

HTTP Methods• HTTP ondersteunt veel methodes– Binnen HTML gebruiken we er maar

twee

• HTTP methodes geven het doel aan– Vertaalt goed naar CRUD acties

Page 21: Excellent rest met de web api

HTTP MethodsAktie HTTP Method

Create POST

Read GET

Update (helemaal vervangen) PUT

Update (gedeeltelijk) PATCH

Delete DELETE

Page 22: Excellent rest met de web api

WebAPI HTTP Methodspublic class DemoController : ApiController { // GET api/demo public IEnumerable<string> Get()

// GET api/demo/5 public string Get(int id)

// POST api/demo public void Post([FromBody]string value)

// PUT api/demo/5 public void Put(int id, [FromBody]string value)

// DELETE api/demo/5 public void Delete(int id)}

Page 23: Excellent rest met de web api

demoCRUD operaties

Page 24: Excellent rest met de web api

Hypermedia - Roy T. Fielding

Hypermedia is defined by the presence of application control information

embedded within, or as a layer above, the presentation of information.

Distributed hypermedia allows the presentation and control information to

be stored at remote locations.Roy T. Fielding

Page 25: Excellent rest met de web api

Richardson Maturity Model

Page 26: Excellent rest met de web api

Het OData Protocol• Open Data Protocol (OData)– Een web protocol voor het zoeken

naar en bijwerken van data.– Gebaseerd op de W3C AtomPub

standaard

• Kan ook metadata bevatten• Zie ook WCF Data Services

Page 27: Excellent rest met de web api

OData metadata• Een OData service kan metadata

teruggeven– http://odata.netflix.com/v2/Catalog/$metad

ata

• Maakt generieke browsers als SQL Server PowerPivot for Excel mogelijk

Page 28: Excellent rest met de web api

demoOData

Page 29: Excellent rest met de web api

ASP.NET WebAPI en OData• Er is een NuGet package

– Install-Package Microsoft.AspNet.WebApi.OData –pre

• Op dit moment is er ondersteuning voor filter en sorteren– Er wordt gewerkt aan het OData

formaat

Page 30: Excellent rest met de web api

OData queriespublic class CustomerController : ApiController{ private NorthwindEntities db = new NorthwindEntities();

// GET api/Default2 [Queryable(PageSize=10)] public IQueryable<Customers> GetCustomers() { return db.Customers; }}

Page 31: Excellent rest met de web api

demoOData queries

Page 32: Excellent rest met de web api
Page 33: Excellent rest met de web api

Conclusie• ASP.NET WebAPI maakt REST

gemakkelijk–Maar soms is MVC al genoeg

• Denk aan hypermedia– Zeker als het een publieke service is