Unleash the power of HTTP with ASP.NET Web API
-
Upload
filip-w -
Category
Technology
-
view
2.006 -
download
7
Transcript of Unleash the power of HTTP with ASP.NET Web API
![Page 1: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/1.jpg)
@filip_woj
Unleash
the power of HTTP
with ASP.NET Web API
![Page 2: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/2.jpg)
@filip_woj
@filip_woj
www.strathweb.com
www.arcmedia.ch
![Page 3: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/3.jpg)
@filip_woj
HTTP
![Page 4: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/4.jpg)
@filip_woj
Trying to build a good API
![Page 5: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/5.jpg)
@filip_woj
What is ASP.NET Web API?
New Microsoft framework (August 2012, Version 2
October 2013) for building HTTP services & applications
Aimed at simplyfing and standardizing HTTP area on the
MS stack (see: WCF / ASMX / MVC / HttpHandlers)
HTTP as a fully fledged *application* protocol
Async from top-to-bottom
Open source!
![Page 6: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/6.jpg)
![Page 7: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/7.jpg)
![Page 8: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/8.jpg)
@filip_woj
“ASP.NET” “Web API”
The name creates lots of misconceptions
“ASP.NET”:
it doesn't require neither ASP.NET, nor IIS to run
“Web API”:
it's an HTTP framework, that can do much more than "just" API
![Page 9: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/9.jpg)
@filip_woj
How can it work without ASP.NET/IIS?
Web hosting:
ASP.NET & IIS
Self hosting: (using WCF hardened core)
WPF
Windows service
console app, any other .NET app
OWIN (i.e. Katana)
Memory hosting: whole pipeline running in memory
![Page 10: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/10.jpg)
![Page 11: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/11.jpg)
@filip_woj
Demo: first API – self hosted
![Page 12: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/12.jpg)
@filip_woj
New HTTP object model
Strongly typed HttpResponseMessage
Strongly typed HttpRequestMessage
Strongly typed Headers
Strongly typed HttpContent
No more “angle brackets coding” & magic strings
Asynchronous API
Client-server symmetry
![Page 13: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/13.jpg)
HTTP status codes
![Page 14: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/14.jpg)
@filip_woj
HTTP status codes
1xx – Informational
2xx – Successful
3xx – Redirection
4xx – Error on the client side
5xx – Error on the server side
![Page 15: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/15.jpg)
![Page 16: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/16.jpg)
@filip_woj
Demo: Created 201 / BadRequest 400
![Page 17: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/17.jpg)
@filip_woj
HTTP headers – request
Request headers (selected)
Accept, Accept-Language, Accept-Encoding
Authorization, Cache-Control, Range
If-Match, If-Modified-Since, If-Range
Content headers (selected)
Allow, Content-Encoding, Content-Language
Content-Length, Content-Type, Expires
Last-Modified
![Page 18: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/18.jpg)
@filip_woj
HTTP headers – response
Response headers (selected)
Accept-Ranges, Connection
Cache-control, Date, Etag
RetryAfter, Warning, Location
Content headers (selected)
Allow, Content-Encoding, Content-Language
Content-Length, Content-Type, Expires
Last-Modified
![Page 19: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/19.jpg)
![Page 20: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/20.jpg)
@filip_woj
RPC
/api/GetTeams
/api/GetTeam?id=1
/api/GetPlayersByTeam?id=1
/api/AddTeam
/api/UpdateTeam
/api/UpdateTeam?id=1&name=”Leafs”
/api/DeleteTeam?id=1
![Page 21: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/21.jpg)
![Page 22: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/22.jpg)
From: presentation by @alexbeletsky
![Page 23: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/23.jpg)
@filip_woj
HTTP verbs
Represent core operations on a resource:
- GET: read, cachable, retreives a resource
- POST: non-cacheable, creates a resource
- PUT: updates a resource, safe to call multiple times
- DELETE: deletes a resource, safe to call multiple times
Can also use others i.e. PATCH (partial update of a
resource)
![Page 24: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/24.jpg)
@filip_woj
RESTful
GET /api/teams
GET /api/teams/1
GET /api/teams/1/players
POST /api/teams
PUT /api/teams/1
DELETE /api/teams/1
![Page 25: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/25.jpg)
@filip_woj
Much better
![Page 26: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/26.jpg)
@filip_woj
Controllers
Represent your API resources
Similar as in MVC
Routing engine determines which controller to use
Web API 2 introduces attribute routing
Supports nested resources
![Page 27: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/27.jpg)
@filip_woj
Dispatching actions
By default HTTP verb based (RESTful)
Actions matched:
by prefix (i.e. GetAll, GetById) or
by action attribute (i.e. [HttpPost])
Can be changed to action based (RPC style)
![Page 28: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/28.jpg)
@filip_woj
Don’t be scared of REST
![Page 29: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/29.jpg)
@filip_woj
Content negotiation
Dynamically deciding the media type
Same data can be represented in various formats:
- JSON
– XML
– CSV
– binary
– anything to what your data can be serialized/deserialized from
In Web API done with MediaTypeFormatters
![Page 30: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/30.jpg)
@filip_woj
Content negotiation in Web API
Web API uses the following precedence:
1. MediaTypeMapping
/api/resource.json, /api/resource?format=json
2. Accept headers
Accept: application/json
3. Content type header
Content-Type: text/xml
4. MediaTypeFormatter order & check whether a formatter can
serialize/deserialize a given type
![Page 31: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/31.jpg)
@filip_woj
Demo: Not Acceptable 406
![Page 32: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/32.jpg)
@filip_woj
Extending content negotiation
Very easy, some examples:
- BSON (binary JSON)
- ServiceStack.Text (instead of JSON.NET)
- MessagePack
- Mobi (returning ebook!)
- RSS/Atom
- Supporting Razor views (text/html)
![Page 33: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/33.jpg)
![Page 34: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/34.jpg)
@filip_woj
Client-server symmetry
By: Henrik Frystyk Nielsen
Message handlers can run on server and on the client
HttpServer is a message handler itself
![Page 35: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/35.jpg)
Sample Handler
![Page 36: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/36.jpg)
@filip_woj
Demo: Memory hosted
![Page 37: Unleash the power of HTTP with ASP.NET Web API](https://reader033.fdocuments.net/reader033/viewer/2022052619/5563a5e6d8b42a2d538b5612/html5/thumbnails/37.jpg)
@filip_woj