Practical ASRNET Web API Badrinarayanan Lakshmiraghavan Apress*
Contents J About the Author About the Technical Reviewer Introduction xiii xv xvii Chapter 1: Building a Basic Web API 1 1.1 Choosing ASP.NET Web API or WCF 2 1.2 Exposing an In-Memory List over HTTP 4 1.3 Choosing Configuration over Convention 9 1.4 Playing by the Rules of HTTP 15 1.4.1 Retrieving Resource(s) 16 1.4.2 Creating a Resource with a Server-Generated Identifier 19 1.4.3 Creating a Resource with a Client-Supplied Identifier 20 1.4.4 Overwriting a Resource 21 1.4.5 Updating a Resource 21 1.4.6 Partially Updating (Patching) a Resource 22 1.4.7 Deleting a Resource 24 Summary 25 Chapter 2: Debugging and Tracing 27 2.1 Using Fiddler for Web Debugging 27 2.2 Capturing Console App Traffic through Fiddler 29 2.3 Capturing HTTPS Traffic in Fiddler 31 2.4 Composing and Submitting Requests in Fiddler 33 2.5 Using F12 Developer Tools in Internet Explorer 36 2.6 Using Developer Tools in Chrome 37 vii
2.7 Enabling ASP.NET Web API Tracing 38 2.8 Creating a Custom Trace Writer 40 2.9 Tracing Entry and Exit 42 2.10 Tracing from Your Code 47 2.11 Tracing Request and Response Messages 51 Summary 53 Chapter 3: Media-Type Formatting CLR Objects 55 3.1 Listing the Out-of-Box Media Formatters 56 3.2 Understanding Conneg 57 3.3 Requesting a Content Type through the Query String 61 3.4 Requesting a Content Type through the Header 63 3.5 Implementing a Custom Media Type Mapping 64 3.6 Overriding Conneg and Returning JSON 66 3.7 Piggybacking on Conneg 66 3.8 Creating a Custom Media Formatter 67 3.9 Extending an Out-of-Box Media Formatter 72 3.10 Controlling Which Members Are Serialized 78 3.10.1 Blacklisting Members 78 3.10.2 Whitelistlng Members 79 3.11 Controlling How Members Are Serialized 80 3.11.1 Controlling Member Names 80 3.11.2 Prettifying JSON 80 3.12 Returning Only a Subset of Members 81 Summary 83 Chapter 4: Customizing Response...85 4.1 Negotiating Character Encoding 85 4.2 Supporting DBCS Character Encoding (Shift JIS) 88 4.3 Negotiating Content Encoding (Compression) 90 viii
4.4 Negotiating Language 100 4.4.1 Internationalizing the Messages to the User 100 4.4.2 Internationalizing the Decimal Separators of Numbers 104 4.4.3 Internationalizing the Dates 110 Summary 113 Chapter 5: Binding an HTTP Request into CLR Objects 115 5.1 Reading the Raw HTTP Request 116 5.2 Reading the HTTP Request into a CLR Type 117 5.3 Binding the HTTP Request to Simple Types 119 5.4 Binding the HTTP Request to Complex Types 121 5.5 Binding the HTTP Request to a Collection 123 5.6 Binding the Form Data 124 5.6.1 Binding to FormDataCollection 125 5.6.2 Binding to Custom Class 128 5.6.3 Binding to a Simple Type 130 5.7 Binding dd/mm/yyyy Dates 132 5.8 Using TypeConverter 136 5.9 Creating a Custom Value Provider 139 5.10 Creating a Custom Model Binder 144 5.11 Creating a Custom Parameter Binder 147 5.12 Creating a Custom Media Formatter 152 Summary 155 Chapter 6: Validating Requests 157 6.1 Validation Using Data Annotations 157 6.2 Handling Validation Errors 162 6.3 Extending an Out-of-the-Box Validation Attribute 168 6.4 Creating Your Own Validation Attribute 170 6.5 Implementing the IValidatableObject Interface 172 Summary 173 ix
Chapter 7: Managing Controller Dependencies 175 7.1 Taking Dependency on the Entity Framework 175 7.2 Inverting Entity Framework Dependencies 182 7.3 Using the Repository Pattern 185 7.4 Using the Generic Repository Pattern 188 7.5 Mapping a Domain to Data Transfer Object (DTO) 195 7.6 Injecting Dependencies Using StructureMap 199 7.7 Unit-Testing the Controller 202 Summary 209 Chapter 8: Extending the Pipeline 211 8.1 Creating a Message Handler 212 8.2 Creating an Exception Filter 218 8.3 Creating an Action Filter to Handle Concurrency 220 8.4 Creating a Controller Selector for Versioning 225 Summary 229 Chapter 9: Hosting ASP.NET Web API 231 9.1 Web Hosting ASP.NET Web API 231 9.2 Self-Hosting ASP.NET Web API 245 9.3 In-Memory Hosting ASP.NET Web API 248 Summary 253 Chapter 10: Securing ASP.NET Web API 255 10.1 Implementing Direct Authentication 255 10.2 Implementing Brokered Authentication 264 10.3 Authorizing Requests 271 Summary 273 X
Chapter 11: Consuming ASP.NET Web API 275 11.1 Calling a Web API from a Console Application 275 11.2 Calling a Web API from a WPF Application 279 11.3 Calling a Web API from JavaScript.. 286 Summary 294 Chapter 12: Building a Performant Web API 295 12.1 Creating Asynchronous Action Methods 295 12.2 Pushing Real-time Updates to the Client... 299 12.3 Implementing Simple Web Caching 306 Summary 314 Index 315 xi