As you might now by now, last month my ASP.NET Web API 2 Recipes book was released by Apress. The book contains over 100 recipes covering various Web API scenarios that aim to help you save some headaches when working on your current or next Web API project.
Each of the recipes has got an accompanying Visual Studio solution, which illustrates the given problem and presents a solution in a simple, isolated manner.
Obviously, it would be great if you went ahead and bought a book (then you would get an in-depth analysis of each case), but the source code itself is available for free at Github and as a download from Apress.
I hope it becomes a useful collection, illustrating how to deal with various Web API problems/scenarios.
Please note that some of the early examples might appear simple or even trivial - that’s the case with introductory recipes, where the gist of the matter is discussed in the book itself.
I have seen in some StackOverflow questions that the examples on Github have already helped a couple of folks, which is fantastic. You can find a summary of the samples below - they are structured in the book and in the repository the same way.
Chapter 1: Web API in ASP.NET π
-
- 1-1 Add ASP.NET Web API to an MVC Application
-
- 1-2 Add ASP.NET Web API to a Web Forms Application
-
- 1-3 Accept an HTML Form
-
- 1-4 Link from MVC Controller to API Controller and Vice Versa
-
- 1-5 Use Scaffolding with ASP.NET Web API
-
- 1-6 Add Model Validation
-
- 1-7 Use CSRF Protection
-
- 1-8 Add Support for Session State
Chapter 2: Web API outside of IIS π
-
- 2-1 Self-Host ASP.NET Web API
-
- 2-2 Host ASP.NET Web API with OWIN
-
- 2-3 Host ASP.NET Web API in Azure Mobile Services
-
- 2-4 Quick Prototypes with scriptcs
-
- 2-5 Host ASP.NET Web API in Azure Worker Role
-
- 2-6 Use ASP.NET Web API with F#
Chapter 3: Routing π
-
- 3-1 Define Centralized Routes
-
- 3-2 Define Direct Routes
-
- 3-3 Set Default Route Values
-
- 3-4 Set Optional Route Values
-
- 3-5 Set Route Constraints
-
- 3-6 Define RPC Style Routes
-
- 3-7 Create Catch-all Routes
-
- 3-8 Prevent Controller Methods from Inadvertently Becoming Web API Endpoints
-
- 3-9 Configure Route-Specific Message Handlers
-
- 3-10 Ignore Roues
-
- 3-11 Localize routes
-
- 3-12 Generate a Link to the Route
Chapter 4: Content Negotiation and Media Types π
-
- 4-1 Request a Specific Media Type from ASP.NET Web API
-
- 4-2 Supporting Additional Media Types
-
- 4-3 Control Model Binding From URI and Body
-
- 4-4 ASP.NET MVC-style Parameter Bindingin ASP.NET Web API
-
- 4-5 Customize XML and JSON Responses
-
- 4-6 Write Your Own MediaTypeFormatter
-
- 4-7 Run Content Negotiation Manually
-
- 4-8 Bypass Content Negotiation
-
- 4-9 Control Media Types with MediaTypeMappings
-
- 4-10 Serve Binary Data from Web API
-
- 4-11 Accept File Uploads
-
- 4-12 Enable Bufferless Uploads
-
- 4-13 Validate File Uploads
Chapter 5: Configuration and Customization π
-
- 5-1 Throttle ASP.NET Web API Calls
-
- 5-2 Use Controllers from an External Assembly
-
- 5-3 Use Controller-Scoped Configuration
-
- 5-4 Validate Input with Action Filters
-
- 5-5 OVerride Filters
-
- 5-6 Add Caching to ASP.NET Web API
-
- 5-7 Use an Existing Caching Library
-
- 5-8 Order Filter Execution with Custom Filters
-
- 5-9 Order Filter Execution Without Custom Filters
-
- 5-10 Customize Error Detail Policy
-
- 5-11 Return HTML from Web API
-
- 5-12 Store Objects for the Duration of HTTP Request
Chapter 6: Embrace HTTP with Web API π
-
- 6-1 Work Directly with HttpRequestMessage
-
- 6-2 Support Partial GET
-
- 6-3 Support the HEAD Verb
-
- 6-4 Support the PATCH Verb
-
- 6-5 Support Batching of HTTP Requests
-
- 6-6 Automatic HTTP 406/Not Acceptable Server Responses
-
- 6-7 Implement Versioning of a Web API with Centralized Routes
-
- 6-8 Implement Versioning of a Web API with Attribute Routing
-
- 6-9 Use Custom HTTP Content
Chapter 7: Exceptions, Troubleshooting and Documenting π
-
- 7-1. Handle Exceptions with Filters
-
- 7-2. Handle Exceptions Globally
-
- 7-3. Log Exceptions Globally
-
- 7-4. Add a Tracer
-
- 7-5. Use an External Tracer
-
- 7-6. Call the Tracer Manually
-
- 7-7. Write a Real-time Tracer
-
- 7-8. Create a Documentation Page for ASP.NET Web API
-
- 7-9. Add Custom Samples to ASP.NET Web API Help Page
-
- 7-10. Add Filters Information to ASP.NET Web API Help Page
-
- 7-11. Support Data Annotations in ASP.NET Web API Help Page
Chapter 8: Cross Domain and Push Communictation π
-
- 8-1 Use JSONP in ASP.NET Web API
-
- 8-2 Use CORS in ASP.NET Web API
-
- 8-3 Create Custom CORS Policies
-
- 8-4 Support Streaming and Push from ASP.NET Web API
-
- 8-5 Support Server-Sent Events in ASP.NET Web API
-
- 8-6 Integrate ASP.NET SignalR into ASP.NET Web API controllers
-
- 8-7 Use WebSockets with ASP.NET Web API
Chapter 9: Dependency Injection π
-
- 9-1. Inject Dependencies into ASP.NET Web API Controllers
-
- 9-2. Add Support for Most Popular DI Containers
-
- 9-3. Deal with Request Scope
-
- 9-4. DI with Other Web API Components
-
- 9-5. Write a Custom DI Adapter (TinyIoC)
Chapter 10: Securing an ASP.NET Web API Service π
-
- 10-1 Use Correct Web API Components for Security-Related Tasks
-
- 10-2 Add HTTPS Support to ASP.NET Web API
-
- 10-3 Use Basic Authentication
-
- 10-4 Integrate Windows Authentication
-
- 10-5 Use the Hawk Authentication Scheme
-
- 10-6 Use OAuth 2.0 with ASP.NET Web API (basic embedded authorization server)
-
- 10-7 Safely Access Current IPrincipal
-
- 10-8 Remove ASP.NET Web API Server Footprint
Chapter 11: Testing Web API Services π
-
- 11-1 Unit Test ASP.NET Web API Controllers
-
- 11-2 Unit Test Message Handlers
-
- 11-3 Unit Test Action Filters
-
- 11-4 Unit Test Formatters
-
- 11-5 Simplify Tests with IHttpActionResult
-
- 11-6 Test Routes
-
- 11-7 Integration Testing
-
- 11-8 Integration Testing with OWIN
Chapter 12: OData (v4) π
-
- 12-1 Creating OData Services in Web API
-
- 12-2 Manage OData Routes
-
- 12-3 Enable OData Queries
-
- 12-4 Support OData Functions and Actions
Free source code π
Available at Github