SlideShare a Scribd company logo
1 of 47
Agenda
• Premessa
• Cos’è e quando si usa WebAPI
• Novità della versione 2.0
• Self Hosting (Owin)
AGENDA
Premessa storica
Quiz: Li riconoscete?
In principio era Internet
Poi fu il Web…
…infine ASP.NET
Web & Internet
Internet
Web
HTTP
Uniform Resource Locator
Metodi HTTP
• GET
• HEAD
• POST
• PUT
• DELETE
• TRACE
• OPTIONS
• CONNECT
• PATCH
• …
The Microsoft Way
ASP.NET
Sites Services
Web
Forms
Web
Pages
MVC
Single
Page
Web
API
SignalR
ASP.NET
WCF
• Henrik Frystyk Nielsen
– First Graduate Student di Tim Berners-Lee
– In W3C coordina le specifiche di HTTP 1.1
– Nel 1999 in Microsoft lavora a SOAP 1.1
– Principal Architect di WCF e WebAPI
AGENDA
Cos’è e quando si usa WebAPI
Web App Vs. Web Service
Web Service: WCF Vs. WebAPI
• WCF SOAP RPC usa HTTP come mero
protocollo di trasporto, definisce specifici
metodi e mette tutta la comunicazione nel
BODY.
• WebAPI consente lo sviluppo di servizi
RESTful, utilizzando i soli metodi HTTP.
RESTful Web Service
• Un servizio RESTful è un Web Service che
implementa i principi REST e utilizza i
metodi HTTP
REpresentational State Transfer
• Client–server
• Stateless
• Uniform interface (metodi HTTP)
• Cacheable
• Layered system
WebAPI
• Adatto alla realizzazione di servizi REST.
• Usa il routing e il binding per mappare la
richiesta HTTP alla corrispondente azione
del controller.
• A differenza di MVC non ha alcuna View.
• E’ l’azione del controller che, utilizzando il
modello, produce direttamente la risposta.
WebAPI Routing
• Attraverso una serie di convenzioni
predefinite (eventualmente modificabili) a
partire dall’URL della richiesta viene
identificato il controller e la rispettiva
azione da eseguire.
WebAPI Binding
• Il binding della richiesta agli eventuali
parametri dell’azione prevede che:
– I dati semplici siano contenuti nell’URL.
– I dati complessi siano racchiusi in un unico
dato complesso contenuto nel BODY della
richiesta.
WebAPI Formatter
• Utilizzati sia nel binding della richiesta sia
nella produzione della risposta.
• URL Formatter:
– per recuperare i parametri semplici dall’URL.
• Body Formatter:
– JSON (che utilizza JSON.NET).
– XML (DataContractSerializer o XMLSerializer).
JSON Vs. XML
• Il tipo di risposta dipende dall’header della
richiesta.
• WebAPI di default risponde in JSON.
• IE tipicamente usa: Accept: text/html, application/xhtml+xml, */*
• Per ricevere la risposta in XML: Accept: application/xml
DEMO
Creiamo un Web Service con WebAPI (1.0 Vs. 2.0)
AGENDA
Novità di WebAPI 2.0
Attribute Routing
• Il lavoro di Tim McCall (autore di
http://attributerouting.net) è stato
incorporato in ASP.NET WebAPI 2.0.
• Con l’attribute routing è possibile definire
le Web API Route come attributi
direttamente nelle Action e Controller.
Attribute Routing: perché?
• Nella vita reale le entità hanno relazioni
(i.e. Clienti/Fatture/Dettaglio fatture).
• Può essere utile, ad esempio, richiedere le
fatture di un cliente: API/Clients/1/Invoices
• Impostare questo tipo di routing diventa
facile con gli Attribute Routing.
Attribute Routing: Casi d’uso
• API versioning
– Versione 1: /api/v1/products
– Versione 2: /api/v2/products
• Overloaded URI segments
– Un singolo ordine: /orders/1
– Un elenco di ordini pendenti: /orders/pending
• Multiple parameter types
– Parametro di tipo intero: /orders/1
– Parametro di tipo data: /orders/2013/06/16
Attribute Routing: Attivazione
IHttpActionResult
DEMO
Attribute Routing e IHttpActionResult
OData
• $select, $expand, $batch e $value
• Estensibilità dei formatter Odata
• Type-less support
• Riutilizzo di un modello EDM esistente
OData
• $select, $expand, $batch e $value
• Estensibilità dei formatter Odata
• IEdmObject (serializer/deserialiazer OData):
– Type-less support
– Riutilizzo di un modello EDM esistente
Request Batching
• ASP.NET Web API ora supporta diverse
strategie di request batching:
– Usare il $batch endpoint di un servizio OData.
– Unire richieste multiple in una singola "MIME
multipart request".
– Usare un batching format personalizzato.
Unit Testing
• Web API 2 rende molto più semplice fare lo
unit test dei controller.
• Basta istanziare il controller con i propri
"request message" e "configuration", e poi
chiamare l’action method che si vuole testare.
• Si può anche moccare la classe UrlHelper, per
testare le azioni che generano link.
Cross Origin Request Sharing
• Grazie al contributo di Brock Allen, ASP.NET
now fully supports CORS
• Browser security prevents a web page from
making AJAX requests to another domain.
CORS is a W3C standard that allows a server
to relax the same-origin policy. Using CORS, a
server can explicitly allow some cross-origin
requests while rejecting others.
Portable ASP.NET Web API Client
• E’ possibile utilizzare ASP.NET Web API
Client per creare librerie PCL utilizzabili per
applicazioni Windows Store and Windows
Phone 8.
• E’ possibile inoltre creare dei formatter
condivisibili tra client e server.
External Authentication
• Il supporto per OAuth 2.0 è basato sul
nuovo middleware di sicurezza Owin.
• In alternativa, si può utilizzare un server di
autorizzazione come Windows Azure
Active Directory o ADFS in Windows
Server 2012 R2
AGENDA
Self Hosting (Owin)
Owin
• Open standard
• Definisce un'interfaccia per lo sviluppo di
moduli semplici e testabili per lo sviluppo
web su .NET
• Disaccoppiamento tra Web server e
framework component
Katana
• Implementazione Microsoft di Owin:
– Server
• HTTP Web Server
– Component
• WebAPI
• SignalR
• OAuth
DEMO
OData - Self Hosting
Q&A
Tutto il materiale di questa sessione su
http://www.communitydays.it/
#CDays13
Grazie a
Sponsor

More Related Content

What's hot

Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente
Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente
Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente Andrea Dottor
 
.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNetMauro Servienti
 
Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Massimo Bonanni
 
.NET Core, ASP.NET Core e Linux per il Mobile
.NET Core, ASP.NET Core e Linux per il Mobile.NET Core, ASP.NET Core e Linux per il Mobile
.NET Core, ASP.NET Core e Linux per il MobilePietro Libro
 
UWP e Mobile App Mobile Services
UWP e Mobile App Mobile ServicesUWP e Mobile App Mobile Services
UWP e Mobile App Mobile ServicesGiuseppe Cramarossa
 
REST API fantastiche e dove trovarle
REST API fantastiche e dove trovarleREST API fantastiche e dove trovarle
REST API fantastiche e dove trovarleMarco Breveglieri
 
Cosa c'è di nuovo in asp.net core 2 0
Cosa c'è di nuovo in asp.net core 2 0Cosa c'è di nuovo in asp.net core 2 0
Cosa c'è di nuovo in asp.net core 2 0Andrea Dottor
 
Microservices webinar EMEA Aug. 2017
Microservices webinar EMEA Aug. 2017Microservices webinar EMEA Aug. 2017
Microservices webinar EMEA Aug. 2017MongoDB
 
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2Pietro Libro
 
Blazor ha vinto? Storie di casi reali
Blazor ha vinto? Storie di casi realiBlazor ha vinto? Storie di casi reali
Blazor ha vinto? Storie di casi realiAndrea Dottor
 
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, PiacenzaSviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, PiacenzaMarco Breveglieri
 
ASP.NET Core - Razor Pages
ASP.NET Core - Razor PagesASP.NET Core - Razor Pages
ASP.NET Core - Razor PagesAndrea Dottor
 
Sviluppare Azure Web Apps
Sviluppare Azure Web AppsSviluppare Azure Web Apps
Sviluppare Azure Web AppsAndrea Dottor
 
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015Kelyon Srl
 

What's hot (19)

Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente
Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente
Back to the Future: Migrare da WebForm ad ASP.NET Core gradualmente
 
.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet
 
Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!Asp.net web form 4.5 - what's new!!
Asp.net web form 4.5 - what's new!!
 
.NET Core, ASP.NET Core e Linux per il Mobile
.NET Core, ASP.NET Core e Linux per il Mobile.NET Core, ASP.NET Core e Linux per il Mobile
.NET Core, ASP.NET Core e Linux per il Mobile
 
UWP e Mobile App Mobile Services
UWP e Mobile App Mobile ServicesUWP e Mobile App Mobile Services
UWP e Mobile App Mobile Services
 
REST API fantastiche e dove trovarle
REST API fantastiche e dove trovarleREST API fantastiche e dove trovarle
REST API fantastiche e dove trovarle
 
Cosa c'è di nuovo in asp.net core 2 0
Cosa c'è di nuovo in asp.net core 2 0Cosa c'è di nuovo in asp.net core 2 0
Cosa c'è di nuovo in asp.net core 2 0
 
Microservices webinar EMEA Aug. 2017
Microservices webinar EMEA Aug. 2017Microservices webinar EMEA Aug. 2017
Microservices webinar EMEA Aug. 2017
 
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2
Visual Studio 2013 - A New kind In Town - ASP.NET Web Api 2
 
Asp net (versione 1 e 2)
Asp net (versione 1 e 2)Asp net (versione 1 e 2)
Asp net (versione 1 e 2)
 
Web sockets
Web socketsWeb sockets
Web sockets
 
Blazor ha vinto? Storie di casi reali
Blazor ha vinto? Storie di casi realiBlazor ha vinto? Storie di casi reali
Blazor ha vinto? Storie di casi reali
 
Asp.Net MVC 5
Asp.Net MVC 5Asp.Net MVC 5
Asp.Net MVC 5
 
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, PiacenzaSviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
Sviluppo Web con React e Delphi - Seminario Delphi Day 2016, Piacenza
 
ASP.NET Core - Razor Pages
ASP.NET Core - Razor PagesASP.NET Core - Razor Pages
ASP.NET Core - Razor Pages
 
Sviluppare Azure Web Apps
Sviluppare Azure Web AppsSviluppare Azure Web Apps
Sviluppare Azure Web Apps
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Tutto su ASP.NET Identity
Tutto su ASP.NET IdentityTutto su ASP.NET Identity
Tutto su ASP.NET Identity
 
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015
Importazione e sincronizzazione di contenuti con Feed Import - SIDCamp 2015
 

Similar to Web api 2.0

ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)Giorgio Di Nardo
 
WeWantWeb - WCF Data Services
WeWantWeb - WCF Data ServicesWeWantWeb - WCF Data Services
WeWantWeb - WCF Data ServicesDomusDotNet
 
Sviluppo di servizi REST per Android - Luca Masini
Sviluppo di servizi REST per Android - Luca Masini Sviluppo di servizi REST per Android - Luca Masini
Sviluppo di servizi REST per Android - Luca Masini Whymca
 
SVILUPPO DI SERVIZI REST PER ANDROID
SVILUPPO DI SERVIZI REST PER ANDROIDSVILUPPO DI SERVIZI REST PER ANDROID
SVILUPPO DI SERVIZI REST PER ANDROIDLuca Masini
 
ASP.NET Core Services e Linux per il Mobile - Pietro Libro - Codemotion Rome...
ASP.NET Core Services e Linux per il Mobile  - Pietro Libro - Codemotion Rome...ASP.NET Core Services e Linux per il Mobile  - Pietro Libro - Codemotion Rome...
ASP.NET Core Services e Linux per il Mobile - Pietro Libro - Codemotion Rome...Codemotion
 
Alessandro Forte - ASP.Net 4.0
Alessandro Forte - ASP.Net 4.0Alessandro Forte - ASP.Net 4.0
Alessandro Forte - ASP.Net 4.0Alessandro Forte
 
ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%DomusDotNet
 
Self hosted Services with .NET OWin
Self hosted Services with .NET OWinSelf hosted Services with .NET OWin
Self hosted Services with .NET OWinNicolò Carandini
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applicationsDotNetCampus
 
Crea servizi REST per la tua App con ASP.NET 5
Crea servizi REST per la tua App con ASP.NET 5Crea servizi REST per la tua App con ASP.NET 5
Crea servizi REST per la tua App con ASP.NET 5Andrea Dottor
 
Architetture web - Linguaggi e standard - Web server, application server, dat...
Architetture web - Linguaggi e standard - Web server, application server, dat...Architetture web - Linguaggi e standard - Web server, application server, dat...
Architetture web - Linguaggi e standard - Web server, application server, dat...Fulvio Corno
 
Le novita di visual studio 2012
Le novita di visual studio 2012Le novita di visual studio 2012
Le novita di visual studio 2012Crismer La Pignola
 
Antica presentazione AJAX
Antica presentazione AJAXAntica presentazione AJAX
Antica presentazione AJAXTommaso Torti
 
Applicazioni web con ASP.NET Owin e Katana
Applicazioni web con ASP.NET Owin e KatanaApplicazioni web con ASP.NET Owin e Katana
Applicazioni web con ASP.NET Owin e KatanaNicolò Carandini
 
Installing Apache tomcat with Netbeans
Installing Apache tomcat with NetbeansInstalling Apache tomcat with Netbeans
Installing Apache tomcat with NetbeansDavide Nardone
 
Web Api – The HTTP Way
Web Api – The HTTP WayWeb Api – The HTTP Way
Web Api – The HTTP WayLuca Milan
 
Applicazioni web based
Applicazioni web basedApplicazioni web based
Applicazioni web basedMarco Liverani
 

Similar to Web api 2.0 (20)

ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)
 
Swagger pertutti
Swagger pertuttiSwagger pertutti
Swagger pertutti
 
WeWantWeb - WCF Data Services
WeWantWeb - WCF Data ServicesWeWantWeb - WCF Data Services
WeWantWeb - WCF Data Services
 
Sviluppo di servizi REST per Android - Luca Masini
Sviluppo di servizi REST per Android - Luca Masini Sviluppo di servizi REST per Android - Luca Masini
Sviluppo di servizi REST per Android - Luca Masini
 
SVILUPPO DI SERVIZI REST PER ANDROID
SVILUPPO DI SERVIZI REST PER ANDROIDSVILUPPO DI SERVIZI REST PER ANDROID
SVILUPPO DI SERVIZI REST PER ANDROID
 
ASP.NET Core Services e Linux per il Mobile - Pietro Libro - Codemotion Rome...
ASP.NET Core Services e Linux per il Mobile  - Pietro Libro - Codemotion Rome...ASP.NET Core Services e Linux per il Mobile  - Pietro Libro - Codemotion Rome...
ASP.NET Core Services e Linux per il Mobile - Pietro Libro - Codemotion Rome...
 
Alessandro Forte - ASP.Net 4.0
Alessandro Forte - ASP.Net 4.0Alessandro Forte - ASP.Net 4.0
Alessandro Forte - ASP.Net 4.0
 
ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%
 
Self hosted Services with .NET OWin
Self hosted Services with .NET OWinSelf hosted Services with .NET OWin
Self hosted Services with .NET OWin
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applications
 
Crea servizi REST per la tua App con ASP.NET 5
Crea servizi REST per la tua App con ASP.NET 5Crea servizi REST per la tua App con ASP.NET 5
Crea servizi REST per la tua App con ASP.NET 5
 
Architetture web - Linguaggi e standard - Web server, application server, dat...
Architetture web - Linguaggi e standard - Web server, application server, dat...Architetture web - Linguaggi e standard - Web server, application server, dat...
Architetture web - Linguaggi e standard - Web server, application server, dat...
 
Le novita di visual studio 2012
Le novita di visual studio 2012Le novita di visual studio 2012
Le novita di visual studio 2012
 
Antica presentazione AJAX
Antica presentazione AJAXAntica presentazione AJAX
Antica presentazione AJAX
 
Applicazioni web con ASP.NET Owin e Katana
Applicazioni web con ASP.NET Owin e KatanaApplicazioni web con ASP.NET Owin e Katana
Applicazioni web con ASP.NET Owin e Katana
 
Net core base
Net core baseNet core base
Net core base
 
Installing Apache tomcat with Netbeans
Installing Apache tomcat with NetbeansInstalling Apache tomcat with Netbeans
Installing Apache tomcat with Netbeans
 
Web Api – The HTTP Way
Web Api – The HTTP WayWeb Api – The HTTP Way
Web Api – The HTTP Way
 
Applicazioni web based
Applicazioni web basedApplicazioni web based
Applicazioni web based
 
Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9
 

More from Nicolò Carandini

Wasm and Blazor CDays keynote
Wasm and Blazor CDays keynoteWasm and Blazor CDays keynote
Wasm and Blazor CDays keynoteNicolò Carandini
 
The absolute need of Secure Http
The absolute need of Secure HttpThe absolute need of Secure Http
The absolute need of Secure HttpNicolò Carandini
 
Christmas greetings cards with blazor
Christmas greetings cards with blazorChristmas greetings cards with blazor
Christmas greetings cards with blazorNicolò Carandini
 
Code review e pair programming con Visual Studio Live Share
Code review e pair programming con Visual Studio Live ShareCode review e pair programming con Visual Studio Live Share
Code review e pair programming con Visual Studio Live ShareNicolò Carandini
 
The Hitchhiker's Guide to the Azure Galaxy
The Hitchhiker's Guide to the Azure GalaxyThe Hitchhiker's Guide to the Azure Galaxy
The Hitchhiker's Guide to the Azure GalaxyNicolò Carandini
 
Web app slots and webapi versioning
Web app slots and webapi versioningWeb app slots and webapi versioning
Web app slots and webapi versioningNicolò Carandini
 
Mobile services multi-piattaforma con Xamarin
Mobile services multi-piattaforma con XamarinMobile services multi-piattaforma con Xamarin
Mobile services multi-piattaforma con XamarinNicolò Carandini
 
Universal Apps localization and globalization
Universal Apps localization and globalizationUniversal Apps localization and globalization
Universal Apps localization and globalizationNicolò Carandini
 
Azure Mobile Services con il .NET Framework
Azure Mobile Services con il .NET FrameworkAzure Mobile Services con il .NET Framework
Azure Mobile Services con il .NET FrameworkNicolò Carandini
 
Sviluppare app per iOS e Android con Xamarin e Visual Studio
Sviluppare app per iOS e Android con Xamarin e Visual StudioSviluppare app per iOS e Android con Xamarin e Visual Studio
Sviluppare app per iOS e Android con Xamarin e Visual StudioNicolò Carandini
 

More from Nicolò Carandini (20)

Wasm and Blazor CDays keynote
Wasm and Blazor CDays keynoteWasm and Blazor CDays keynote
Wasm and Blazor CDays keynote
 
The absolute need of Secure Http
The absolute need of Secure HttpThe absolute need of Secure Http
The absolute need of Secure Http
 
Christmas greetings cards with blazor
Christmas greetings cards with blazorChristmas greetings cards with blazor
Christmas greetings cards with blazor
 
Xamarin DevOps
Xamarin DevOpsXamarin DevOps
Xamarin DevOps
 
Code review e pair programming con Visual Studio Live Share
Code review e pair programming con Visual Studio Live ShareCode review e pair programming con Visual Studio Live Share
Code review e pair programming con Visual Studio Live Share
 
Azure dev ops meetup one
Azure dev ops meetup oneAzure dev ops meetup one
Azure dev ops meetup one
 
Spa with Blazor
Spa with BlazorSpa with Blazor
Spa with Blazor
 
The Hitchhiker's Guide to the Azure Galaxy
The Hitchhiker's Guide to the Azure GalaxyThe Hitchhiker's Guide to the Azure Galaxy
The Hitchhiker's Guide to the Azure Galaxy
 
Game matching with SignalR
Game matching with SignalRGame matching with SignalR
Game matching with SignalR
 
Swagger loves WebAPI
Swagger loves WebAPISwagger loves WebAPI
Swagger loves WebAPI
 
Xamarin Workbooks
Xamarin WorkbooksXamarin Workbooks
Xamarin Workbooks
 
Swagger per tutti
Swagger per tuttiSwagger per tutti
Swagger per tutti
 
Web app slots and webapi versioning
Web app slots and webapi versioningWeb app slots and webapi versioning
Web app slots and webapi versioning
 
Intro xamarin forms
Intro xamarin formsIntro xamarin forms
Intro xamarin forms
 
Windows 10 design
Windows 10 designWindows 10 design
Windows 10 design
 
Windows 10 IoT
Windows 10 IoTWindows 10 IoT
Windows 10 IoT
 
Mobile services multi-piattaforma con Xamarin
Mobile services multi-piattaforma con XamarinMobile services multi-piattaforma con Xamarin
Mobile services multi-piattaforma con Xamarin
 
Universal Apps localization and globalization
Universal Apps localization and globalizationUniversal Apps localization and globalization
Universal Apps localization and globalization
 
Azure Mobile Services con il .NET Framework
Azure Mobile Services con il .NET FrameworkAzure Mobile Services con il .NET Framework
Azure Mobile Services con il .NET Framework
 
Sviluppare app per iOS e Android con Xamarin e Visual Studio
Sviluppare app per iOS e Android con Xamarin e Visual StudioSviluppare app per iOS e Android con Xamarin e Visual Studio
Sviluppare app per iOS e Android con Xamarin e Visual Studio
 

Web api 2.0

  • 1.
  • 2. Agenda • Premessa • Cos’è e quando si usa WebAPI • Novità della versione 2.0 • Self Hosting (Owin)
  • 5. In principio era Internet
  • 6. Poi fu il Web…
  • 10. Web
  • 11. HTTP
  • 13. Metodi HTTP • GET • HEAD • POST • PUT • DELETE • TRACE • OPTIONS • CONNECT • PATCH • …
  • 16. WCF • Henrik Frystyk Nielsen – First Graduate Student di Tim Berners-Lee – In W3C coordina le specifiche di HTTP 1.1 – Nel 1999 in Microsoft lavora a SOAP 1.1 – Principal Architect di WCF e WebAPI
  • 17. AGENDA Cos’è e quando si usa WebAPI
  • 18. Web App Vs. Web Service
  • 19. Web Service: WCF Vs. WebAPI • WCF SOAP RPC usa HTTP come mero protocollo di trasporto, definisce specifici metodi e mette tutta la comunicazione nel BODY. • WebAPI consente lo sviluppo di servizi RESTful, utilizzando i soli metodi HTTP.
  • 20. RESTful Web Service • Un servizio RESTful è un Web Service che implementa i principi REST e utilizza i metodi HTTP
  • 21. REpresentational State Transfer • Client–server • Stateless • Uniform interface (metodi HTTP) • Cacheable • Layered system
  • 22. WebAPI • Adatto alla realizzazione di servizi REST. • Usa il routing e il binding per mappare la richiesta HTTP alla corrispondente azione del controller. • A differenza di MVC non ha alcuna View. • E’ l’azione del controller che, utilizzando il modello, produce direttamente la risposta.
  • 23. WebAPI Routing • Attraverso una serie di convenzioni predefinite (eventualmente modificabili) a partire dall’URL della richiesta viene identificato il controller e la rispettiva azione da eseguire.
  • 24. WebAPI Binding • Il binding della richiesta agli eventuali parametri dell’azione prevede che: – I dati semplici siano contenuti nell’URL. – I dati complessi siano racchiusi in un unico dato complesso contenuto nel BODY della richiesta.
  • 25. WebAPI Formatter • Utilizzati sia nel binding della richiesta sia nella produzione della risposta. • URL Formatter: – per recuperare i parametri semplici dall’URL. • Body Formatter: – JSON (che utilizza JSON.NET). – XML (DataContractSerializer o XMLSerializer).
  • 26. JSON Vs. XML • Il tipo di risposta dipende dall’header della richiesta. • WebAPI di default risponde in JSON. • IE tipicamente usa: Accept: text/html, application/xhtml+xml, */* • Per ricevere la risposta in XML: Accept: application/xml
  • 27. DEMO Creiamo un Web Service con WebAPI (1.0 Vs. 2.0)
  • 29. Attribute Routing • Il lavoro di Tim McCall (autore di http://attributerouting.net) è stato incorporato in ASP.NET WebAPI 2.0. • Con l’attribute routing è possibile definire le Web API Route come attributi direttamente nelle Action e Controller.
  • 30. Attribute Routing: perché? • Nella vita reale le entità hanno relazioni (i.e. Clienti/Fatture/Dettaglio fatture). • Può essere utile, ad esempio, richiedere le fatture di un cliente: API/Clients/1/Invoices • Impostare questo tipo di routing diventa facile con gli Attribute Routing.
  • 31. Attribute Routing: Casi d’uso • API versioning – Versione 1: /api/v1/products – Versione 2: /api/v2/products • Overloaded URI segments – Un singolo ordine: /orders/1 – Un elenco di ordini pendenti: /orders/pending • Multiple parameter types – Parametro di tipo intero: /orders/1 – Parametro di tipo data: /orders/2013/06/16
  • 34. DEMO Attribute Routing e IHttpActionResult
  • 35. OData • $select, $expand, $batch e $value • Estensibilità dei formatter Odata • Type-less support • Riutilizzo di un modello EDM esistente
  • 36. OData • $select, $expand, $batch e $value • Estensibilità dei formatter Odata • IEdmObject (serializer/deserialiazer OData): – Type-less support – Riutilizzo di un modello EDM esistente
  • 37. Request Batching • ASP.NET Web API ora supporta diverse strategie di request batching: – Usare il $batch endpoint di un servizio OData. – Unire richieste multiple in una singola "MIME multipart request". – Usare un batching format personalizzato.
  • 38. Unit Testing • Web API 2 rende molto più semplice fare lo unit test dei controller. • Basta istanziare il controller con i propri "request message" e "configuration", e poi chiamare l’action method che si vuole testare. • Si può anche moccare la classe UrlHelper, per testare le azioni che generano link.
  • 39. Cross Origin Request Sharing • Grazie al contributo di Brock Allen, ASP.NET now fully supports CORS • Browser security prevents a web page from making AJAX requests to another domain. CORS is a W3C standard that allows a server to relax the same-origin policy. Using CORS, a server can explicitly allow some cross-origin requests while rejecting others.
  • 40. Portable ASP.NET Web API Client • E’ possibile utilizzare ASP.NET Web API Client per creare librerie PCL utilizzabili per applicazioni Windows Store and Windows Phone 8. • E’ possibile inoltre creare dei formatter condivisibili tra client e server.
  • 41. External Authentication • Il supporto per OAuth 2.0 è basato sul nuovo middleware di sicurezza Owin. • In alternativa, si può utilizzare un server di autorizzazione come Windows Azure Active Directory o ADFS in Windows Server 2012 R2
  • 43. Owin • Open standard • Definisce un'interfaccia per lo sviluppo di moduli semplici e testabili per lo sviluppo web su .NET • Disaccoppiamento tra Web server e framework component
  • 44. Katana • Implementazione Microsoft di Owin: – Server • HTTP Web Server – Component • WebAPI • SignalR • OAuth
  • 45. DEMO OData - Self Hosting
  • 46. Q&A Tutto il materiale di questa sessione su http://www.communitydays.it/ #CDays13

Editor's Notes

  1. Inserite l’eventuale vostro logo in basso a destra
  2. Dal 1973 al 1978 Cerf ha condotto con Robert Kahn la ricerca che sviluppò e collaudò i protocolli di comunicazione TCP/IP e la stessa rete Internet come la conosciamo noi oggi. Alla fine del 1982 i nuovi protocolli, collaudati, erano in grado di sostituire i vecchi protocolli nel progetto ARPANET. La data fissata per il passaggio fu il 1º gennaio 1983.
  3. Inventore dell’ipertesto, di HTTP e HTML
  4. Internet (sviluppato dal 1973 al 1978, sostituisce DARPANET nel 1981) WWW (nasce al CERN nel 1990 con una proposta per un finanziamento della ricerca, NON approvato!)
  5. I due elementi principali, costituenti del Web sono HTTP e HTML
  6. GET: Requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. HEAD: Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. POST: Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, as examples, an annotation for existing resources; a message for a bulletin board, newsgroup, mailing list, or comment thread; a block of data that is the result of submitting a web form to a data-handling process; or an item to add to a database. PUT: Requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI. DELETE: Deletes the specified resource. TRACE: Echoes back the received request so that a client can see what (if any) changes or additions have been made by intermediate servers. OPTIONS: Returns the HTTP methods that the server supports for the specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource. CONNECT: Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy. PATCH: Is used to apply partial modifications to a resource.[
  7. Scott Guthrie è l’uomo chiave di ASP.NET ASP.NET 1.0: Gennaio 2002 ASP.NET 1.1 con Scott Guthrie: Aprile 2003
  8. Il trasporto è internet, la comunicazione è HTTP, il tutto qui è dunque il Web. La divisione lato client è più confusa di quello che viene qui schematizzato, lato server è semplice: Da una parte viene prodotta la pagina Web, da l’altra dati in formato JSON / XML
  9. GET: Requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. HEAD: Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. POST: Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, as examples, an annotation for existing resources; a message for a bulletin board, newsgroup, mailing list, or comment thread; a block of data that is the result of submitting a web form to a data-handling process; or an item to add to a database. PUT: Requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI. DELETE: Deletes the specified resource. TRACE: Echoes back the received request so that a client can see what (if any) changes or additions have been made by intermediate servers. OPTIONS: Returns the HTTP methods that the server supports for the specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource. CONNECT: Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy. PATCH: Is used to apply partial modifications to a resource.[
  10. GET: Requests a representation of the specified resource. Requests using GET should only retrieve data and should have no other effect. HEAD: Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. POST: Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI. The data POSTed might be, as examples, an annotation for existing resources; a message for a bulletin board, newsgroup, mailing list, or comment thread; a block of data that is the result of submitting a web form to a data-handling process; or an item to add to a database. PUT: Requests that the enclosed entity be stored under the supplied URI. If the URI refers to an already existing resource, it is modified; if the URI does not point to an existing resource, then the server can create the resource with that URI. DELETE: Deletes the specified resource. TRACE: Echoes back the received request so that a client can see what (if any) changes or additions have been made by intermediate servers. OPTIONS: Returns the HTTP methods that the server supports for the specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource. CONNECT: Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy. PATCH: Is used to apply partial modifications to a resource.[
  11. Ultima slide, obbligatoria
  12. Slide da mostrare prima di iniziare la sessione – non rimuovere!