Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
The ASP.NET Web API for Beginners
-
Upload
kevin-hazzard -
Category
Technology
-
view
1.954 -
download
1
description
Transcript of The ASP.NET Web API for Beginners
The ASP.NET Web API for Beginners
W. Kevin Hazzard
Richardson Maturity Model
martinfowler.com/articles/richardsonMaturityModel.html
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
H A T E O A S
Hypermedia As
The Engine Of
ApplicationState
H A T E O A S
Web API Architecture
Web API Processing Architecture
controller
message
hostingHttpRequestM
essa
ge
Http
ResponseMess
age
HTTP Request
GET /index.html HTTP/1.1Accept: text/htmlAccept-Encoding: gzip, deflateAccept-Language: en-USUser-Agent: Mozilla/5.0Connection: Keep-Alive
HttpRequestMessage
• In System.Net.Http namespace• Properties:
oContent – HttpContentoHeaders – HttpRequestHeadersoMethod – HttpMethodo Properties – IDictionary<string, object>oRequestUri – Uri
Request Extensions• CreateErrorResponse – many overloads• CreateResponse – many overloads• GetClientCertificate• GetProperty<T>• GetQueryNameValuePairs• GetUrlHelper
These are in System.Net.Http.dll.
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=/
...
HttpResponseMessage
• In System.Net.Http namespace• Properties:
oContent – HttpContentoHeaders – HttpResponseHeaderso IsSuccessStatusCode – booloReasonString – stringoRequestMessage – HttpRequestMessageo StatusCode – HttpStatusCode
Key Attributeso HttpGeto HttpPosto HttpPuto HttpPatcho HttpDeleteo HttpHeado HttpOptions
o AcceptVerbso Authorizeo AllowAnonymouso NonActiono FromBodyo FromUrio Queryable
Example OneCreate a Simple Controller to Fetch Person Entities
Add OData Query Syntax Support
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
Example TwoAdd WebApiTestClient to the Project and ConfigureTurn Documentation Comments on and Configure
Cross-Cutting Concerns
HttpMessageHandler class:
protected abstract Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken token);
DelegatingHandler
Derives fromHttpMessageHandler
Chains handlers togetherin the order youadd them
Chained Handlers
Server
MessageHandle
rA
Message
HandlerB
Dispatch
Message
HandlerSendAsync SendAsync SendAsync
Example ThreeImplement an Authorization Key Handler
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
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
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
Example FourAdd Tests
Example Four Summary
• Add test fixtures• Demonstrate Arrange, Act, Assert pattern• Call controller action without HttpRequestMethod• Call controller action with HttpRequestMethod
Contacting [email protected]@KevinHazzard
blogs.captechconsulting.commanning.com/hazzard