KEMBAR78
The ASP.NET Web API for Beginners | PPTX
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
Application State
H A T E O A S
Web API Architecture
Web API Processing Architecture
HttpRequestMessage
HttpResponseMessag
e
HTTP Request
GET /index.html HTTP/1.1
Accept: text/html
Accept-Encoding: gzip, deflate
Accept-Language: en-US
User-Agent: Mozilla/5.0
Connection: Keep-Alive
HttpRequestMessage
• In System.Net.Http namespace
• Properties:
o Content – HttpContent
o Headers – HttpRequestHeaders
o Method – HttpMethod
o Properties – IDictionary<string, object>
o RequestUri – 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 OK
Cache-Control: private, max-age=0
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Date: Thu, 31 Dec 2015 23:59:59 GMT
Content-Length: 41309
Connection: keep-alive
Set-Cookie: XYZ=123; domain=.me.com; path=/
...
HttpResponseMessage
• In System.Net.Http namespace
• Properties:
o Content – HttpContent
o Headers – HttpResponseHeaders
o IsSuccessStatusCode – bool
o ReasonString – string
o RequestMessage – HttpRequestMessage
o StatusCode – HttpStatusCode
Key Attributes
o HttpGet
o HttpPost
o HttpPut
o HttpPatch
o HttpDelete
o HttpHead
o HttpOptions
o AcceptVerbs
o Authorize
o AllowAnonymous
o NonAction
o FromBody
o FromUri
o Queryable
Example One
Create 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 Two
Add WebApiTestClient to the Project and Configure
Turn Documentation Comments on and Configure
Cross-Cutting Concerns
HttpMessageHandler class:
protected abstract
Task<HttpResponseMessage>
SendAsync(
HttpRequestMessage request,
CancellationToken token);
DelegatingHandler
Derives from
HttpMessageHandler
Chains handlers together
in the order you
add them
Chained Handlers
Server
SendAsync SendAsync SendAsync
Example Three
Implement 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 HttpContext
o WFC produces an OperationContext
• Typically requires a running host
Example Four
Add Tests
Example Four Summary
• Add test fixtures
• Demonstrate Arrange, Act, Assert pattern
• Call controller action without HttpRequestMethod
• Call controller action with HttpRequestMethod
Contacting Kevin
wkhazzard@gmail.com
@KevinHazzard
blogs.captechconsulting.com
manning.com/hazzard

The ASP.NET Web API for Beginners

Editor's Notes

  • #6 Values idealism over pragmatism and app longevity over short-term efficiencyResult: nobody does it correctly