Web API Best Practices
Web API Best Practices
NET Core
Web API Best Practices
STEVE SMITH
ARDALIS.COM | @ARDALIS | STEVE@ARDALIS.COM
WEEKLYDEVTIPS.COM (PODCAST)
Open standards-based
Technology-agnostic
REST-based APIs are stateless; each request may be handled by a different server-side resource
https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design
Keep it SIMPLE
Keep it CONSISTENT
API design, including URIs, may not map 1:1 to domain objects or database tables.
Example:
POST /orders
May map to a NewOrderRequest on the server that triggers processing payment, checking
inventory, sending notifications, etc.
Or it could just insert a record in the Orders table.
It should be able to do either without the API changing.
TOO MUCH
/customers/1/orders/123 (instead: /orders/123)
/customers/1/orders/123/items/1/products/2 (instead: /products/2)
Example:
GET /customers/1
Response includes customer data, as well as links to:
Update the customer Delete the customer List customer orders
List customer addresses Add an address Add an order
An idempotent HTTP method can be called multiple times without changing the expected
response.
*Decide if a DELETE for a missing id should return a 404 or not. If it returns 404, it won’t be Idempotent.
http://restcookbook.com/HTTP%20Methods/idempotency/
Create your own BaseApiController like this one (consider also adding Authorize
filter):
If specifying ID on DTOs, may not make sense to use for new object requests (POSTs)
◦ Consider having separate NewResourceDTO and ResourceDTO types
◦ ResourceDTO can inherit from NewResourceDTO and simply add the Id property
REST services should (typically) return the resource in the body of POST commands, not
redirect
Encoding-Specific
◦ return Json(model); // JsonResult
Integration Tests
◦ Test several methods and/or classes working together
◦ Useful for verifying infrastructure code works correctly
Functional Tests
◦ Test full application stack
◦ Slowest, often most brittle, but provide greatest confidence a particular user scenario works fully
Learn more:
https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2
Consider whether you will version your entire API (simplest) or resource by resource (generally
not recommended).
Avoid making breaking changes to your API as much as possible. No versioning option is
without its problems.
Well-known
Steps
◦ Authenticate user and issue token. Store in client (local storage for browser).
◦ Add token in header on subsequent requests
◦ Validate token on server using middleware; return 401 if not valid
Other Resources
• Microsoft Architecture eBook/sample http://aka.ms/WebAppArchitecture
• Securing Web API in ASP.NET Core
• http://www.blinkingcaret.com/2017/09/06/secure-web-api-in-asp-net-core/