Agenda
• ASP.NET WebAPI pipeline overview
• Message Handlers (DelegatingHandler)
• Filters (ActionFilterAttribute)
• Formatters (MediaTypeFormatter)
• Exception Handling
• ExceptionLogger
• ExceptionHandler
• Caching
• Streaming (upload & download)
• What’s new in Web API 2
ASP.NET Web API is a framework that makes it
easy to build HTTP services that reach a broad
range of clients, including browsers and mobile
devices. It is an ideal platform for building
RESTful applications on the .NET Framework
ASP.NET WebAPI
ASP.NET WebAPI pipeline
HostMessage Handlers
Controller
Model Binding
------------Result
conversion
Action Method
HTTP message handlers are the first stage in the
processing pipeline. They process HTTP request
messages on the way in, and HTTP response
messages on the way out.
• Derive from DelegatingHandler
• Global or per-route handlers
Message Handlers
Message Handlers
Delegating handler #1
Delegating handler #2
HttpRoutingDispatcher
HttpControllerDispatcher
Host Controller
Filters are great way to add extra functionality to your
Web API service or to secure your controllers and
actions.
WebAPI filters != MVC filters
Filters
Filters
Authentication filter
Authorization filter
Action filtersControllerAction
Method
Model Binding
Result conversion
Result conversion – out of the box formatters for JSON/XML/BSON (v2)Ability to easy write your own custom formatters (results) – CSV, vCard …
• Client error – HTTP 4xx
• Server error – HTTP 5xx
• Avoid try/catch in controller actions
Error handling
Error handling
ExceptionHandler
• Handler for catching
exception in pipeline
• One handler at the time
ExceptionLogger
• Handler for logging the
exceptions
• Multiple handlers at the
time
IMPORTANT : Doesn’t catch exception caused by:
Serialization errors, controller initialization, pre/post controller
Http caching - client
Cache-Control
• no-cache (default)
• no-store
• max-age (TTL, seconds)
• Private (no store at proxies)
Expires
• Default value (-1)
• Date of expiration
– UTC
– Can be year from now
– max-age from Cache-
Control have advantage
Http caching - server• WebAPI doesn’t support OutputCache MVC filter
• Two possibilities
– Manually adding caching header to response (Etag value)
– Use third party stuff (CacheCow, OutputCache…)
Http caching - ETag• Response header name – Etag
• Request header name – If-None-Match
Client sends Etag inside
request headers
Server compare
received Etagwith resource
Etag value
RESPONSE – HTTP 304 (Not modified)
Server returns updated resource with new Etag
Client caches response
==
!=
Chunked transfer - download
System.Net.Http.StreamContent
• Pull data direct from the
file
• Avoids creating local byte
array data to hold the file
System.Net.Http.PushStreamContent
• Size is not known
• Use stream Flush() to send
chunk of data to the client
Chunked transfer - upload
System.Net.Http.HttpContent.ReadAsStreamAsync
System.Net.Http.HttpContent.ReadAsStreamMultipartAsync
• Non-browser file upload
• Stream data direct to the file on the server
• Browser file upload (form data)
What’s new in WebAPI 2
• Out of the box support for BSON using JSON.NET library
• Attribute routing (Route and RoutePrefix)
• Odata improvements
• CORS (Cross Origin Request Sharing)
• New IHttpResult response
– return Ok();
– return NotFound(…);
Resources
• http://www.asp.net/web-api
• http://www.asp.net/posters/web-api/asp.net-web-api-
poster.pdf
• http://channel9.msdn.com/Events/TechEd/Europe/2014/D
EV-B410
• Stackoverflow.com, Google, MSDN…
@rjovic
Source code : https://github.com/rjovic/Windays2015
Top Related