web api best practices - sdd...
TRANSCRIPT
![Page 2: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/2.jpg)
About me• Microsoft MVP
• Asp.NET Insider
• Web API Advisor
• Pluralsight contributor
• Dev Chair – DevConnections
• Conference Organizer – MVPMIX.com
• Conference Organizer - jsSaturday
• Leader – Rockstar Developers Meetup Austin
• Leader – .NET user group at UTDallas
• President– Chander Dhall, Inc.
![Page 3: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/3.jpg)
Agenda• REST Principles
• Web API – Best Practices
• A Typical Web API Pipeline
3
![Page 4: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/4.jpg)
• Participation == :-)
• No participation == :-(
![Page 5: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/5.jpg)
ChanderDhall.com/Podcasts
![Page 6: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/6.jpg)
Free Resharper
• http://ChanderDhall.com/CodeCamp
![Page 7: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/7.jpg)
Step 1
Appserver & DBServer
Database ServerApp Server
![Page 8: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/8.jpg)
Awesome Solution?
Load BalancerLoad
Balancer
DB
DB
DB
DB Cluster
Has
h Ma
p
DB
DB
DB
DB Cluster
Load BalancerLoad
Balancer
DB
DB
DB
DB Cluster
Has
h Ma
p
DB
DB
DB
DB Cluster
Set 1-10 Million Users Set 11-20 Million Users
Global Redirector
Global
Look up Hash
Map
![Page 9: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/9.jpg)
May be close to an awesome
solution?Load Balancer
Load
Balancer
DB
DB
DB
DB Cluster
Hash
Map
DB
DB
DB
DB Cluster
Master
Slave Slave
SANNo Sql
Master
Slave Slave
Search Db
CachingOffline
Processing
![Page 10: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/10.jpg)
Why?
• Amazon claim – Just an extra 1/10th of a second
on their response times will cost them 1% in sales.
• Google – ½ a second increase in latency caused
traffic to drop by a fifth.
![Page 11: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/11.jpg)
1. REST Principles
Principle 1 - Client-server● Clients are not concerned with data storage,
which remains internal to each server.
● Servers are not concerned with the user
interface or user state.
● Servers and clients may also be replaced
and developed independently, as long as
the interface between them is not altered.
![Page 12: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/12.jpg)
1. REST Principles
Principle 2 - Stateless● The client–server communication is further
constrained by no client context being
stored on the server between requests.
![Page 13: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/13.jpg)
No sql paradigm – Materialized
pathsElectronics
TV Phones Computers Cameras
Samsung Apple LG
LCD LED
![Page 14: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/14.jpg)
No sql paradigm – Materialized
pathsTV
Samsung Apple LG
LCD LED
{ “entity”: “TV”,
“category”:”Electronics”}{ “entity”: “Samsung”, “category”:”Electronics, TV”}
{ “entity”: “Samsung”, “category”:”Electronics, TV, LCD”}
![Page 15: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/15.jpg)
1. REST Principles
Principle 3 - Cacheable● Clients can cache responses.● Responses must therefore, implicitly or
explicitly, define themselves as cacheable, or not, to prevent clients reusing stale or inappropriate data in response to further requests.
● Well-managed caching partially or completely eliminates some client–server interactions, further improving scalability and performance.
![Page 16: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/16.jpg)
1. REST Principles
Principle 4 - Layered system● A client cannot ordinarily tell whether it is
connected directly to the end server, or to
an intermediary along the way.
● Intermediary servers may improve system
scalability by enabling load-balancing and
by providing shared caches.
![Page 17: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/17.jpg)
No sql paradigm – Nested sets
Electronics
TV Phones
Samsung Sony Cell Landline1 2 3 4 5 6 7 8 9 10 11 12 13 14
![Page 18: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/18.jpg)
1. REST Principles
Principle 5 - Code on demand● Servers are able temporarily to extend or
customize the functionality of a client by
the transfer of executable code.
![Page 19: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/19.jpg)
1. REST Principles
Principle 6 - Uniform Interface● The uniform interface between clients and
servers, discussed below, simplifies and
decouples the architecture, which enables
each part to evolve independently.
![Page 20: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/20.jpg)
http://ChanderDhall.com/gettrainings
GET
http://ChanderDhall.com/trainings
Principle: Use HTTP verbs
20
Nouns, No Verbs in the URL
![Page 21: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/21.jpg)
What about?
• Responses that don’t involve resources
• Use verbs
• Example:
http://ChanderDhall.com/translate?from=en
&to=gn
![Page 22: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/22.jpg)
http://ChanderDhall.com/trainings
http://ChanderDhall.com/trainings/34
Principle: 2 base
URLs/resource
22
KISS (Keep it simple, stupid!)
![Page 23: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/23.jpg)
• HTTP Verbs
GET
http://ChanderDhall.com/trainings
Retrieves all trainings
GET
http://ChanderDhall.com/trainings/23
Retrieves trainings with Id = 2323
GET
![Page 24: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/24.jpg)
• HTTP Verbs
POST
http://ChanderDhall.com/trainings
Creates a new training(Strange?)
POST
http://ChanderDhall.com/trainings/23
Error – Why, so?
24
POST
![Page 25: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/25.jpg)
• HTTP Verbs
PUT
http://ChanderDhall.com/trainings
Bulk update trainings
PUT
http://ChanderDhall.com/trainings/23
If exists, update 23
Else ERROR
25
PUT
![Page 26: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/26.jpg)
• HTTP Verbs
DELETE
http://ChanderDhall.com/trainings
Delete all trainings
DELETE
http://ChanderDhall.com/trainings/23
If exists, Delete 23
Else Resource Not found Error
26
DELETE
![Page 27: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/27.jpg)
Associations
GET
/trainers/12/trainings/144
27
![Page 28: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/28.jpg)
GET
/trainers/12/trainings?zip=926
18&tech=api
28
Complexity
![Page 29: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/29.jpg)
Error Format
• Error format 1
{
“code" : "401",
"message“ : "Authenticate",
}
29
![Page 30: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/30.jpg)
Error Format
• Error format 2
{
“type" : “OAuthException",
"message“ : “(#401: Request not authorized",
}
30
![Page 31: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/31.jpg)
Error Format
• Error format 3
{
"status" : "401",
"message“ : "Authenticate",
"code“ : 1234,
“details":
"http://www.chanderdhall.com/docs/errors/1234"
}
31
(Recommended!)
![Page 32: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/32.jpg)
Error Format
• Google GData 200 201 304 400 401 403
404 409 410 500
• Netflix 200 201 304 400 401 403 404 412
500
• Digg 200 400 401 403 404 410 500 503
![Page 33: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/33.jpg)
Errors
• Only 3 cases
• Everything worked
• Client did something wrong
• API did something wrong
33
![Page 34: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/34.jpg)
Error Codes (Required)
• So minimum response codes
• 200 - OK
• 400 - Bad Request
• 500 - Internal Server Error
34
![Page 35: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/35.jpg)
Error Codes (Extended)
• 201 - Created
• 304 - Not Modified
• 404 – Not Found
• 401 - Unauthorized
• 403 - Forbidden
35
![Page 36: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/36.jpg)
Versioning
• /bankName/v2.0/accounts/checking
• /2013-09-10/bankName/accounts/checkingThis
• /bankName/accounts/checking?v=1
36
![Page 37: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/37.jpg)
Versioning
• Timestamp: Is the version based on the
date of launch or date of creation?
• Version in URL: Interface appears to
change sooner that it actually might.
• Version is optional: (Red Flag)
37
![Page 38: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/38.jpg)
Versioning
• Versioning is mandatory
• Make version the first field if you want
from the left (v1/trainings)
• How many versions to maintain?
• URLs or Headers?
38
![Page 39: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/39.jpg)
Versioning
• Accept-Datetime: Thu, 1 Oct 2013
20:35:00 GMT
• Cookie: $Version=1; Skin=new;
• ETag:
"737060cd8c284d8af7ad3082f209582d"
39
![Page 40: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/40.jpg)
Pagination
• http://chanderdhall.com/podcasts?page
=3&rpp=5
• http://chanderdhall.com/podcasts?start=
3&count=5
40
![Page 41: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/41.jpg)
Pagination
• http://chanderdhall.com/podcasts?offset
=3&limit=5
• Don’t forget to include Default Pagination
41
![Page 42: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/42.jpg)
Multiple Formats
• http://chanderdhall.com/podcasts?type=json
Or
http://chanderdhall.com/podcasts?type=xml
• http://chanderdhall.com/podcasts.json
Or http://chanderdhall.com/podcasts.xml
42
![Page 43: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/43.jpg)
Multiple Formats
• http://chanderdhall.com/podcasts
accept: application/json
43
![Page 44: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/44.jpg)
Responses
a. "Created_At": "2013-10-10T04:35:00Z"
b. "DateTime": "2013-10-10T04:35:00Z"
44
![Page 45: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/45.jpg)
Responses
var podcast = JSON.parse(response);
podcast.createdAt
45
![Page 46: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/46.jpg)
P
I
P
E
L
I
N
E
W
E
B
A
P
I
ASP.NET Hosting Self Hosting
HttpControllerHandler
HttpServer HttpSelfHostServer
DelegatingHandler
HttpRoutingDispatcher
HttpControllerDispatcher
HttpRequestMessage HttpResponse
Message
If(Route.Handler) Yes Route.Handler
DelegatingHandler
HttpMessageHandler
No
1. Create API Controller
Message Handlers
![Page 47: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/47.jpg)
W
E
B
A
P
I
Action Filters
2. Select Controller ActionP
I
P
E
L
I
N
E
Controller
Can create an error response if request is not
authorized3. Model Binding
Authorization Filters
Action filters are invoked
twice
4. Result Conversion
5. Invoke Action
OnActionExecuting OnActionExecuted
Controller Action
Exception Filters
![Page 48: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/48.jpg)
3. Model Binding
HttpRequestMessage
URI Headers Body
IModelBinder
ModelBinderParameterBinding
IValueProvider
Simple Type Any Type
HttpParameterBinding
Complex Type
MediaTypeFormatter
FormatterParameterBinding
![Page 49: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/49.jpg)
4. Result Conversion
HttpResponseMessage
void
If(returnType== void)
return (204)
HttpResponseMessage
If(returnType == HttpResponseMessage)
Pass through
Other Types
IContentNegotiator
MediaTypeFormatter
![Page 50: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/50.jpg)
Features you should know about
• Documentation
![Page 51: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/51.jpg)
Features you should know about
• Test Client (Nuget)
• Install-Package WebApiTestClient
![Page 52: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/52.jpg)
Features you should know about
• External Authentication Services
● Microsoft Accounts.
• And more
![Page 53: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/53.jpg)
Features you should know about
• Unit Testing
![Page 54: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/54.jpg)
Features you should know about
• Enable Cross Origin Requests
Install-Package
Microsoft.AspNet.WebApi.Cors -pre -
project WebService
![Page 55: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/55.jpg)
Enable Cross Origin Requests
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var cors = new
EnableCorsAttribute("www.example.com", "*", “*");
config.EnableCors(cors);
// ...
}
}
Behind the scenes
Access-Control-Allow-Origin: http://www.example.com
Access-Control-Allow-Headers: x-my-custom-header
Access-Control-Allow-Methods: PUT
![Page 56: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/56.jpg)
Enable CORS (Method Level)
public class ItemsController : ApiController
{
public HttpResponseMessage GetAll() { ... }
[EnableCors(origins: "http://www.example.com",
headers: "*", methods: "*")]
public HttpResponseMessage GetItem(int id) { ... }
public HttpResponseMessage Post() { ... }
public HttpResponseMessage PutItem(int id) { ... }
}
![Page 57: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/57.jpg)
Enable CORS (Controller level)
[EnableCors(origins: "http://www.example.com", headers:
"*", methods: "*")]
public class ItemsController : ApiController
{
public HttpResponseMessage GetAll() { ... }
public HttpResponseMessage GetItem(int id) { ... }
public HttpResponseMessage Post() { ... }
[DisableCors]
public HttpResponseMessage PutItem(int id) { ... }
}
![Page 58: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/58.jpg)
Enable CORS (Custom)public class MyCorsPolicyAttribute : Attribute, ICorsPolicyProvider{
private CorsPolicy _policy;
public MyCorsPolicyAttribute(){
// Create a CORS policy._policy = new CorsPolicy{
AllowAnyMethod = true,AllowAnyHeader = true
};
// Add allowed origins._policy.Origins.Add("http://myclient.azurewebsites.net");_policy.Origins.Add("http://www.contoso.com");
}
public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request)
{return Task.FromResult(_policy);
}}
![Page 59: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/59.jpg)
Web API 2 (New Features)
• Attribute Routing
![Page 60: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/60.jpg)
Attribute Routing
[Route("api/books")]
public IEnumerable<Book> GetBooks() { ... }
[Route("api/books/{id:int}")]
public Book GetBook(int id) { ... }
[Route("api/books")]
public HttpResponseMessage CreateBook(Book
book) { ... }
![Page 61: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/61.jpg)
Attribute Routing
[RoutePrefix("api/books")]
public class BooksController : ApiController
{
// GET api/books
[Route("")]
public IEnumerable<Book> Get() { ... }
// GET api/books/5
[Route("{id:int}")]
public Book Get(int id) { ... }
// POST api/books
[Route("")]
public HttpResponseMessage Post(Book book) { ... }
}
![Page 62: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/62.jpg)
Attribute Routing
[RoutePrefix("api/books")]
public class BooksController : ApiController
{
// GET /api/authors/1/books
[Route("~/api/authors/{authorId:int}/books")]
public IEnumerable<Book> GetByAuthor(int authorId) {
... }
// ...
}
‘~’ Overrides the RoutePrefix
![Page 63: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/63.jpg)
Attribute Routing
[RoutePrefix("customers/{customerId}")]
public class OrdersController : ApiController
{
// GET customers/1/orders
[Route("orders")]
public IEnumerable<Order> Get(int customerId) { ... }
}
![Page 64: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/64.jpg)
Attribute Routing
[Route("users/{id:int}"]
public User GetUserById(int id) { ... }
[Route("users/{name}"]
public User GetUserByName(string name) { ... }
First route chosen if the id is an int.
Otherwise, second.
![Page 65: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/65.jpg)
Constraint Description Example
alphaMatches uppercase or lowercase Latin
alphabet characters (a-z, A-Z){x:alpha}
bool Matches a Boolean value. {x:bool}
datetime Matches a DateTime value. {x:datetime}
decimal Matches a decimal value. {x:decimal}
double Matches a 64-bit floating-point value. {x:double}
float Matches a 32-bit floating-point value. {x:float}
guid Matches a GUID value. {x:guid}
int Matches a 32-bit integer value. {x:int}
lengthMatches a string with the specified length or
within a specified range of lengths.
{x:length(6)}
{x:length(1,20)}
long Matches a 64-bit integer value. {x:long}
max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
range Matches an integer within a range of values. {x:range(10,50)}
regex Matches a regular expression. {x:(^\d{3}-\d{3}-\d{4}$)}
![Page 66: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/66.jpg)
Attribute Routing
[Route("users/{id:int:min(1), id:int:max(10)}")]
public User GetUserById(int id) { ... }
Multiple constraints can be applied separated by a
comma
![Page 67: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/67.jpg)
No sql paradigm – Index table
Employee Id Details
1234 Email: [email protected]; State: CA; Dept: IT
8235 Email: [email protected]; State: TX; Dept: Sales
2234 Email: [email protected]; State: AL; Dept: IT
1671 Email: [email protected]; State: WA; Dept: Sales
State Employee Id
CA 1234, 1235, 1236, 1244
TX 8000, 8100, 8235, 8266
AL 2212, 2221, 2234, 2256
Dept Employee Id
IT 1234, 1235, 1236, 1244
Sales 8000, 8100, 8235, 8266
Acc 2212, 2221, 2234, 2256
![Page 68: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/68.jpg)
No sql paradigm – Tree Index
Country - USA
State - CA
City - LA
Properties
Facilities
{
“property”:
[{ “facilityName”:
“abc”,
“facilityId”:”111”},
{“facilityName”:”
xyz” ,
“facilityId”:”222”}]
}
![Page 69: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/69.jpg)
No sql paradigm – Composite Key
Dept= IT:* or
Dept= Sales:Online:*
IT: Software: 1123 EmpName: John; Address: Los Angeles
IT: Software: 2323 EmpName: Kevin; Address: Dallas, TX
IT: Hardware: 6767 EmpName: Matt; Address: San Francisco
Sales: Online: 832 EmpName: Katie: Address: Austin, Tx
Sales : Online: 423 EmpName: Karen: Address: Irvine, CA
Sales : Store : 556 EmpName: Richard; Address: San Diego
IT
Employees
Sales
Employ
ees
E
M
P
L
O
Y
E
E
S
![Page 70: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/70.jpg)
No sql paradigm - Grouping
U123: O111Product Ids: [“Surface”,
“xbox”]U124:O123 Product Ids: [“Win 8”, “xbox”]
U124:O234 Product Ids: [“Win phone”, “surface”]
U124:O999Product Ids: [“office”, “azure
sub”]U125:O789
Product Ids: [“msdn”,
“office”]U125:O945
Product Ids: [“surface”,
“xbox”]
Colocation of a
users’ data.
GroupBy clause
![Page 71: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/71.jpg)
No sql paradigm – Inverted search
& direct aggregation
EmpId, dept, city, …….
Dept-IT: [111, 123, 234….]
Dept-Sales:[673, 343, 434….]
City: Dallas
City: LA
111: Dept-Sales, City: LA …
222: Dept-IT, City: Dallas ….
![Page 72: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/72.jpg)
No sql paradigm – Materialized
pathsElectronics
TV Phones Computers Cameras
Samsung Apple LG
LCD LED
![Page 73: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/73.jpg)
No sql paradigm – Materialized
pathsTV
Samsung Apple LG
LCD LED
{ “entity”: “TV”,
“category”:”Electronics”}{ “entity”: “Samsung”, “category”:”Electronics, TV”}
{ “entity”: “Samsung”, “category”:”Electronics, TV, LCD”}
![Page 74: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/74.jpg)
No sql paradigm – Nested sets
Electronics
TV Phones
Samsung Sony Cell Landline1 2 3 4 5 6 7 8 9 10 11 12 13 14
![Page 75: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/75.jpg)
No sql paradigm – Nested sets
1 2 3 4 5 6 7 8 9 10 11 12 13
SonySamsung
TV
LandlineCell
Phone
Electronics
![Page 76: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/76.jpg)
No sql paradigm : Flattening nested
documents
Name:
Chander
Hadoop: Expert
Nodejs: Expert
Spanish: Novice
{
“name”:”chander”,
“skills”:”hadoop, nodejs,
Spanish”,
“level”:”expert, expert,
novice”
}
Skills:hadoop AND
level:expert
![Page 77: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/77.jpg)
No sql paradigm : Flattening nested
documents
Name: Chander
Hadoop: Expert
Nodejs: Expert
Spanish: Novice
{“name”:”chander”,“skills_1”:”hadoop”,“skills_2”: “nodejs”,“skills_3”: “spanish”, “level_1”:”expert”,“level_2”: “expert”,“level_3”: “novice”}
![Page 78: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/78.jpg)
Search
78
![Page 79: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/79.jpg)
Formatted results
79
![Page 80: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/80.jpg)
Hypermedia
80
![Page 81: Web API Best Practices - SDD Conferencesddconf.com/brands/sdd/library/Web_API_Best_Practices.pdfAbout me • Microsoft MVP • Asp.NET Insider • Web API Advisor • Pluralsight contributor](https://reader036.vdocument.in/reader036/viewer/2022071210/602210553add1719dd0e17e8/html5/thumbnails/81.jpg)
Resources
• http://chanderdhall.com/CodeCamp
• Twitter @csdhall
• http://apigee.com