SlideShare uma empresa Scribd logo
1 de 33
Excellent REST met de
WebAPI
Maurice de Beijer
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:    mauricedb@computer.org
Agenda
• Wat is REST?
• Wat is de ASP.NET WebAPI
• Hypermedia
Wat is REST?

  REpresentational State Transfer (REST) is
       een software-architectuur voor
  gedistribueerde mediasystemen zoals het
              World wide web.
                   Wikipedia
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
Hypertext Transfer Protocol
The 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
Richardson Maturity Model
ASP.NET WebAPI
ASP.NET Web API is een framework dat
het makkelijk maakt om HTTP en REST
    services te bouwen op het .NET
               framework.
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
WebAPI Controllers
• Veel controle over data naar de client
  – HttpResponseMessage
  – Content negotiation
• Kan ook HTTP headers zetten
  – Caching
  – Optimistic concurrency
WebAPI Controllers
public class DemoController : ApiController
{
    // GET api/demo
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}
WebAPI Routes
• Routes koppelen URL aan Controller
  – Net als in ASP.NET MVC
• Je kan er meerdere hebben
  – De volgorde is van belang
WebAPI Routes
public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}
demo
WebAPI
Content negotiation
• Wat we versturen != hoe we het
  weergeven
  – JSON of XML: een boek blijft een boek
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
MediaTypeFormatter
public 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)
    {
        // ...
    }
}
demo
Content Negotiation
HTTP Methods
• HTTP ondersteunt veel methodes
  – Binnen HTML gebruiken we er maar twee
• HTTP methodes geven het doel aan
  – Vertaalt goed naar CRUD acties
HTTP Methods
Aktie                         HTTP Method
Create                        POST
Read                          GET
Update (helemaal vervangen)   PUT
Update (gedeeltelijk)         PATCH
Delete                        DELETE
WebAPI HTTP Methods
public 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)
}
demo
CRUD operaties
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
Richardson Maturity Model
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
OData metadata
• Een OData service kan metadata
  teruggeven
  – http://odata.netflix.com/v2/Catalog/$metadata
• Maakt generieke browsers als SQL
  Server PowerPivot for Excel mogelijk
demo
OData
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
OData queries
public class CustomerController : ApiController
{
    private NorthwindEntities db = new NorthwindEntities();

    // GET api/Default2
    [Queryable(PageSize=10)]
    public IQueryable<Customers> GetCustomers()
    {
        return db.Customers;
    }
}
demo
OData queries
Conclusie
• ASP.NET WebAPI maakt REST
  gemakkelijk
  – Maar soms is MVC al genoeg
• Denk aan hypermedia
  – Zeker als het een publieke service is

Mais conteúdo relacionado

Destaque

Overview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIOverview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIPankaj Bajaj
 
WCF tutorial
WCF tutorialWCF tutorial
WCF tutorialAbhi Arya
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVCMohd Manzoor Ahmed
 
C# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTC# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTDr. Awase Khirni Syed
 
Data Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesData Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesArvind Krishnaa
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonAdnan Masood
 
Twitter For Reflection Jan 2020
Twitter For Reflection Jan 2020Twitter For Reflection Jan 2020
Twitter For Reflection Jan 2020Tony McNeill
 

Destaque (10)

Overview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIOverview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB API
 
WCF tutorial
WCF tutorialWCF tutorial
WCF tutorial
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC
 
C# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENTC# ASP.NET WEB API APPLICATION DEVELOPMENT
C# ASP.NET WEB API APPLICATION DEVELOPMENT
 
Data Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesData Binding and Data Grid View Classes
Data Binding and Data Grid View Classes
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
 
Twitter For Reflection Jan 2020
Twitter For Reflection Jan 2020Twitter For Reflection Jan 2020
Twitter For Reflection Jan 2020
 
Windows Communication Foundation (WCF)
Windows Communication Foundation (WCF)Windows Communication Foundation (WCF)
Windows Communication Foundation (WCF)
 

Semelhante a Excellent rest met de web api

HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo BruggeHTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo BruggePureplexity
 
General Drupal presentation in Dutch
General Drupal  presentation in DutchGeneral Drupal  presentation in Dutch
General Drupal presentation in DutchRoel Meester
 
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, Hummel
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, HummelDSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, Hummel
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, HummelDeltares
 
Lucius Drupal Development Cursus
Lucius Drupal Development CursusLucius Drupal Development Cursus
Lucius Drupal Development CursusLuciuswebsystems
 
Vertaling Seo Concepten Naar Implementatie
Vertaling Seo Concepten Naar ImplementatieVertaling Seo Concepten Naar Implementatie
Vertaling Seo Concepten Naar Implementatieefocus.im
 
Drupal Uitgebreide Starters Training
Drupal Uitgebreide Starters TrainingDrupal Uitgebreide Starters Training
Drupal Uitgebreide Starters TrainingLuciuswebsystems
 
Rollbase via de REST adapter koppelen met OpenEdge
Rollbase via de REST adapter koppelen met OpenEdgeRollbase via de REST adapter koppelen met OpenEdge
Rollbase via de REST adapter koppelen met OpenEdgeJoris Kroes
 
Sitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlRuud van Falier
 
DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)dpc
 
Drupal Cursus Hans Rossel
Drupal Cursus Hans RosselDrupal Cursus Hans Rossel
Drupal Cursus Hans RosselHans Rossel
 
Lucius Websystems Drupal Startersdag
Lucius Websystems Drupal StartersdagLucius Websystems Drupal Startersdag
Lucius Websystems Drupal StartersdagLuciuswebsystems
 
Case Automatisering Gids Sitecore Event 12062008
Case Automatisering Gids Sitecore Event 12062008Case Automatisering Gids Sitecore Event 12062008
Case Automatisering Gids Sitecore Event 12062008efocus.im
 
Ict2 trm- werking internet
Ict2 trm- werking internetIct2 trm- werking internet
Ict2 trm- werking internetkaatversele
 

Semelhante a Excellent rest met de web api (20)

HTML5 & rest services
HTML5 & rest servicesHTML5 & rest services
HTML5 & rest services
 
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo BruggeHTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
HTML 5, ASP.NET MVC & Windows Azure sessie voor Ivo Brugge
 
Modern web development
Modern web developmentModern web development
Modern web development
 
General Drupal presentation in Dutch
General Drupal  presentation in DutchGeneral Drupal  presentation in Dutch
General Drupal presentation in Dutch
 
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, Hummel
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, HummelDSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, Hummel
DSD-NL 2018 Delft-FEWS & Web Services - Van Dijk, Ekkelenkamp, Hummel
 
HTML5 Overview
HTML5 OverviewHTML5 Overview
HTML5 Overview
 
Lucius Drupal Development Cursus
Lucius Drupal Development CursusLucius Drupal Development Cursus
Lucius Drupal Development Cursus
 
Vertaling Seo Concepten Naar Implementatie
Vertaling Seo Concepten Naar ImplementatieVertaling Seo Concepten Naar Implementatie
Vertaling Seo Concepten Naar Implementatie
 
Drupal Uitgebreide Starters Training
Drupal Uitgebreide Starters TrainingDrupal Uitgebreide Starters Training
Drupal Uitgebreide Starters Training
 
Rollbase via de REST adapter koppelen met OpenEdge
Rollbase via de REST adapter koppelen met OpenEdgeRollbase via de REST adapter koppelen met OpenEdge
Rollbase via de REST adapter koppelen met OpenEdge
 
Sitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nlSitecore - Onder de motorkop van ParTechIT.nl
Sitecore - Onder de motorkop van ParTechIT.nl
 
HTML5 (Dutch)
HTML5 (Dutch)HTML5 (Dutch)
HTML5 (Dutch)
 
DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)DPC2007 Case Study Surfnet (Herman Van Dompseler)
DPC2007 Case Study Surfnet (Herman Van Dompseler)
 
Api kooien les 1
Api kooien les 1Api kooien les 1
Api kooien les 1
 
Drupal Cursus Hans Rossel
Drupal Cursus Hans RosselDrupal Cursus Hans Rossel
Drupal Cursus Hans Rossel
 
Lucius Websystems Drupal Startersdag
Lucius Websystems Drupal StartersdagLucius Websystems Drupal Startersdag
Lucius Websystems Drupal Startersdag
 
Webservices
WebservicesWebservices
Webservices
 
Case Automatisering Gids Sitecore Event 12062008
Case Automatisering Gids Sitecore Event 12062008Case Automatisering Gids Sitecore Event 12062008
Case Automatisering Gids Sitecore Event 12062008
 
Debat Wegwijs in het landschap van archiefbeheersysteem
Debat Wegwijs in het landschap van archiefbeheersysteemDebat Wegwijs in het landschap van archiefbeheersysteem
Debat Wegwijs in het landschap van archiefbeheersysteem
 
Ict2 trm- werking internet
Ict2 trm- werking internetIct2 trm- werking internet
Ict2 trm- werking internet
 

Mais de Maurice De Beijer [MVP]

Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components AppPractice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components AppMaurice De Beijer [MVP]
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software ProjectA foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software ProjectMaurice De Beijer [MVP]
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using CypressSurati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using CypressMaurice De Beijer [MVP]
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using CypressBuild reliable Svelte applications using Cypress
Build reliable Svelte applications using CypressMaurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureMaurice De Beijer [MVP]
 
Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18Maurice De Beijer [MVP]
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and AzureBuilding reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and AzureMaurice De Beijer [MVP]
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with ReactBuilding large and scalable mission critical applications with React
Building large and scalable mission critical applications with ReactMaurice De Beijer [MVP]
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureMaurice De Beijer [MVP]
 
Building reliable web applications using Cypress
Building reliable web applications using CypressBuilding reliable web applications using Cypress
Building reliable web applications using CypressMaurice De Beijer [MVP]
 
Getting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent renderingGetting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent renderingMaurice De Beijer [MVP]
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockMaurice De Beijer [MVP]
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScriptFrom zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScriptMaurice De Beijer [MVP]
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptMaurice De Beijer [MVP]
 
Create flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apisCreate flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apisMaurice De Beijer [MVP]
 

Mais de Maurice De Beijer [MVP] (20)

Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components AppPractice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components App
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software ProjectA foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software Project
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using CypressSurati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using CypressBuild reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and AzureBuilding reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and Azure
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with ReactBuilding large and scalable mission critical applications with React
Building large and scalable mission critical applications with React
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
Building reliable web applications using Cypress
Building reliable web applications using CypressBuilding reliable web applications using Cypress
Building reliable web applications using Cypress
 
Getting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent renderingGetting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent rendering
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScriptFrom zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
The new React
The new React The new React
The new React
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
I am hooked on React
I am hooked on ReactI am hooked on React
I am hooked on React
 
Create flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apisCreate flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apis
 

Excellent rest met de web api

  • 1.
  • 2. Excellent REST met de WebAPI Maurice de Beijer
  • 3. 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: mauricedb@computer.org
  • 4. Agenda • Wat is REST? • Wat is de ASP.NET WebAPI • Hypermedia
  • 5. Wat is REST? REpresentational State Transfer (REST) is een software-architectuur voor gedistribueerde mediasystemen zoals het World wide web. Wikipedia
  • 6. 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
  • 7. Hypertext Transfer Protocol The 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
  • 9. ASP.NET WebAPI ASP.NET Web API is een framework dat het makkelijk maakt om HTTP en REST services te bouwen op het .NET framework.
  • 10. 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
  • 11. WebAPI Controllers • Veel controle over data naar de client – HttpResponseMessage – Content negotiation • Kan ook HTTP headers zetten – Caching – Optimistic concurrency
  • 12. WebAPI Controllers public class DemoController : ApiController { // GET api/demo public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } }
  • 13. WebAPI Routes • Routes koppelen URL aan Controller – Net als in ASP.NET MVC • Je kan er meerdere hebben – De volgorde is van belang
  • 14. WebAPI Routes public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
  • 16. Content negotiation • Wat we versturen != hoe we het weergeven – JSON of XML: een boek blijft een boek
  • 17. 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
  • 18. MediaTypeFormatter public 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) { // ... } }
  • 20. HTTP Methods • HTTP ondersteunt veel methodes – Binnen HTML gebruiken we er maar twee • HTTP methodes geven het doel aan – Vertaalt goed naar CRUD acties
  • 21. HTTP Methods Aktie HTTP Method Create POST Read GET Update (helemaal vervangen) PUT Update (gedeeltelijk) PATCH Delete DELETE
  • 22. WebAPI HTTP Methods public 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) }
  • 24. 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
  • 26. 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
  • 27. OData metadata • Een OData service kan metadata teruggeven – http://odata.netflix.com/v2/Catalog/$metadata • Maakt generieke browsers als SQL Server PowerPivot for Excel mogelijk
  • 29. 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
  • 30. OData queries public class CustomerController : ApiController { private NorthwindEntities db = new NorthwindEntities(); // GET api/Default2 [Queryable(PageSize=10)] public IQueryable<Customers> GetCustomers() { return db.Customers; } }
  • 32.
  • 33. Conclusie • ASP.NET WebAPI maakt REST gemakkelijk – Maar soms is MVC al genoeg • Denk aan hypermedia – Zeker als het een publieke service is