Download presentation
Presentation is loading. Please wait.
Published byOsborn Merritt Modified over 8 years ago
1
ASP.NET WEB API Napredne tehnike i mogućnosti RENATO JOVIĆ, Tagit Adriatica d.o.o.
2
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
3
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
4
ASP.NET WebAPI pipeline Host Message Handlers Controller Model Binding ------------ Result conversion Action Method
5
Journey of HTTP message
6
MESSAGE HANDLERS
7
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
8
Delegating handler #1 Delegating handler #2 HttpRouting Dispatcher HttpController Dispatcher Host Controller
9
MESSAGE HANDLERS Demo
10
FILTERS
11
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
12
Authentication filter Authorization filter Action filters Controller Action 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 …
13
FILTERS & FORMATTERS Demo
14
ERROR HANDLING
15
Client error – HTTP 4xx Server error – HTTP 5xx Avoid try/catch in controller actions Error handling
16
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
17
EXCEPTION HANDLING Demo
18
HTTP CACHING
19
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
20
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…)
21
Http caching - ETag Response header name – Etag Request header name – If-None-Match Client sends Etag inside request headers Server compare received Etag with resource Etag value RESPONSE – HTTP 304 (Not modified) Server returns updated resource with new Etag Client caches response == !=
22
HTTP CACHING Demo
23
HTTP STREAMING
24
Chunked transfer - download System.Net.Http.StreamCont ent Pull data direct from the file Avoids creating local byte array data to hold the file System.Net.Http.PushStreamCon tent Size is not known Use stream Flush() to send chunk of data to the client
25
Chunked transfer - upload System.Net.Http.HttpContent.ReadAsStreamAsync System.Net.Http.HttpContent.ReadAsStreamMultipartA sync Non-browser file upload Stream data direct to the file on the server Browser file upload (form data)
26
HTTP STREAMING Demo
27
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(…);
28
renato@tagitinc.com @rjovic
29
Nagrađujemo vas sa 100 WinCoin bodova što ste posjetili predavanje. Osvojite dodatnih 100 WinCoin bodova ukoliko popunite službeni upitnik. HVALA!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.