The ASP.NET Web API for Beginners

25
The ASP.NET Web API for Beginners W. Kevin Hazzard

description

An introduction to the ASP.NET Web API with a focus on RESTful design.

Transcript of The ASP.NET Web API for Beginners

Page 1: The ASP.NET Web API for Beginners

The ASP.NET Web API for Beginners

W. Kevin Hazzard

Page 2: The ASP.NET Web API for Beginners

Richardson Maturity Model

martinfowler.com/articles/richardsonMaturityModel.html

Page 3: The ASP.NET Web API for Beginners

The Web API Experience

• Solid resource-orientation• HTTP method support• Content negotiation• Open-ended formatting• Model-binding• Minimal platform

dependencies• Test friendly

Level 3

Level 2

Level 1

Level 0

Page 4: The ASP.NET Web API for Beginners

H A T E O A S

Hypermedia As

The Engine Of

ApplicationState

H A T E O A S

Page 5: The ASP.NET Web API for Beginners

Web API Architecture

Page 6: The ASP.NET Web API for Beginners

Web API Processing Architecture

controller

message

hostingHttpRequestM

essa

ge

Http

ResponseMess

age

Page 7: The ASP.NET Web API for Beginners

HTTP Request

GET /index.html HTTP/1.1Accept: text/htmlAccept-Encoding: gzip, deflateAccept-Language: en-USUser-Agent: Mozilla/5.0Connection: Keep-Alive

Page 8: The ASP.NET Web API for Beginners

HttpRequestMessage

• In System.Net.Http namespace• Properties:

oContent – HttpContentoHeaders – HttpRequestHeadersoMethod – HttpMethodo Properties – IDictionary<string, object>oRequestUri – Uri

Page 9: The ASP.NET Web API for Beginners

Request Extensions• CreateErrorResponse – many overloads• CreateResponse – many overloads• GetClientCertificate• GetProperty<T>• GetQueryNameValuePairs• GetUrlHelper

These are in System.Net.Http.dll.

Page 10: The ASP.NET Web API for Beginners

HTTP Response

HTTP/1.1 200 OKCache-Control: private, max-age=0Content-Type: text/html; charset=utf-8Vary: Accept-EncodingDate: Thu, 31 Dec 2015 23:59:59 GMTContent-Length: 41309Connection: keep-aliveSet-Cookie: XYZ=123; domain=.me.com; path=/

...

Page 11: The ASP.NET Web API for Beginners

HttpResponseMessage

• In System.Net.Http namespace• Properties:

oContent – HttpContentoHeaders – HttpResponseHeaderso IsSuccessStatusCode – booloReasonString – stringoRequestMessage – HttpRequestMessageo StatusCode – HttpStatusCode

Page 12: The ASP.NET Web API for Beginners

Key Attributeso HttpGeto HttpPosto HttpPuto HttpPatcho HttpDeleteo HttpHeado HttpOptions

o AcceptVerbso Authorizeo AllowAnonymouso NonActiono FromBodyo FromUrio Queryable

Page 13: The ASP.NET Web API for Beginners

Example OneCreate a Simple Controller to Fetch Person Entities

Add OData Query Syntax Support

Page 14: The ASP.NET Web API for Beginners

Example One Summary

• Implement a basic controller with a actions• Demonstrate controller selection by convention• Discuss controller selection by attribution• Implement OData query parameters and

demonstrate

Page 15: The ASP.NET Web API for Beginners

Example TwoAdd WebApiTestClient to the Project and ConfigureTurn Documentation Comments on and Configure

Page 16: The ASP.NET Web API for Beginners

Cross-Cutting Concerns

HttpMessageHandler class:

protected abstract Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken token);

Page 17: The ASP.NET Web API for Beginners

DelegatingHandler

Derives fromHttpMessageHandler

Chains handlers togetherin the order youadd them

Page 18: The ASP.NET Web API for Beginners

Chained Handlers

Server

MessageHandle

rA

Message

HandlerB

Dispatch

Message

HandlerSendAsync SendAsync SendAsync

Page 19: The ASP.NET Web API for Beginners

Example ThreeImplement an Authorization Key Handler

Page 20: The ASP.NET Web API for Beginners

Example Three Summary

• Implement an application key handler• Discuss the invocation of the InnerHandler• Demonstrate the creation and return of an error

response• Discuss why throwing exceptions will always

return an HTTP 500 (Internal Server Error) result• Demonstrate using the request object to create

the error response instead• Attach the handler to the pipeline• Debug with Help & Test

Page 21: The ASP.NET Web API for Beginners

Possible Uses• Implementing a cache manager• Implementing an AAA scheme• Capturing pay-per-call data• Recording statistics• Logging and tracing• Inserting custom request and response headers• Performing message compression or encryption• Transforming messages

Page 22: The ASP.NET Web API for Beginners

Testing Web API• Faking context with ASMX and WCF is difficult• Too many platform dependencies

o ASP.NET produces an HttpContexto WFC produces an OperationContext

• Typically requires a running host

Page 23: The ASP.NET Web API for Beginners

Example FourAdd Tests

Page 24: The ASP.NET Web API for Beginners

Example Four Summary

• Add test fixtures• Demonstrate Arrange, Act, Assert pattern• Call controller action without HttpRequestMethod• Call controller action with HttpRequestMethod

Page 25: The ASP.NET Web API for Beginners

Contacting [email protected]@KevinHazzard

blogs.captechconsulting.commanning.com/hazzard