20
ASP.NET Web API 2 Recipes A Problem-Solution Approach Filip Wojcieszyn Apress-

ASP.NET Web API 2 recipes : a problem-solution … 365 V. Contents ... Support DataAnnotations in ASP.NETWebAPI Help Page 231 Problem 231 ... [hands-on examples for building feature-rich

Embed Size (px)

Citation preview

ASP.NET Web API 2

Recipes

A Problem-Solution Approach

Filip Wojcieszyn

Apress-

Contents at a Glance

About the Author xxv

About the Technical Reviewer xxvii

Acknowledgments xxix

Introduction xxxi

Chapter 1: Web API in ASP.NET 1

Chapter 2: ASP.NET Web API Outside of IIS 33

Chapter 3: Routing 57

Chapter 4: Content Negotiation and Media Types 91

Chapter 5: Configuration and Customization 129

Chapter 6: Embrace HHP with ASP.NET Web API 171

Chapter 7: Exceptions, Troubleshooting, and Documenting 205

Chapter 8: Cross Domain and Push Communication 235

Chapter 9: Dependency Injection 269

Chapter 10: Securing an ASP.NET Web API Service 283

Chapter 11: Testing Web API Services 317

Chapter 12: OData 347

Index 365

V

Contents

About the Author xxv

About the Technical Reviewer xxvii

Acknowledgments xxix

Introduction xxxi

Chapter 1: Web API in ASP.NET 1

1 -1. Add ASP.NET Web API to an MVC Application 1

Problem 1

Solution 1

How It Works 2

The Code 3

1-2. Add ASP.NET Web API to a Web Forms Application 5

Problem 5

Solution 5

How It Works 6

The Code 7

1-3. Accept an HTML Form 9

Problem 9

Solution 9

How It Works 9

The Code 10

1-4. Link from MVC Controller to API Controller and Vice Versa 12

Problem 12

Solution 12

vii

CONTENTS

How It Works 12

The Code 13

1 -5. Use Scaffolding with ASP.NET Web API 15

Problem 15

Solution 15

How It Works 15

The Code 15

1-6. Add Model Validation 20

Problem 20

Solution 20

How It Works 21

The Code 22

1-7. Use CSRF Protection 25

Problem 25

Solution 25

How It Works 25

The Code 25

1-8. Add Support for Session State 28

Problem 28

Solution 28

How It Works 29

The Code 29

Chapter 2: ASP.NET Web API Outside of IIS 33

2-1. Self-Host ASP.NET Web API 33

Problem : 33

Solution 33

How It Works 34

The Code 36

2-2. Host ASP.NET Web API with OWIN 37

Problem 37

Solution 37

viii

CONTENTS

How It Works 37

The Code 40

2-3. Host ASP.NET Web API in Azure Mobile Services 41

Problem 41

Solution 41

How It Works 43

The Code 46

2-4. Quick Prototypes with scriptcs 48

Problem 48

Solution 48

How It Works 49

The Code 49

2-5. Host ASP.NET Web API in Azure Worker Role 50

Problem 50

Solution 50

How It Works 51

The Code 52

2-6. Use ASP.NET Web API with F# 53

Problem 53

Solution 53

How It Works 54

The Code 55

Chapter 3: Routing 57

3- 1. Define Centralized Routes 57

Problem 57

Solution 57

How It Works 58

The Code 59

3-2. Define Direct Routes 61

Problem 61

Solution 61

ix

CONTENTS

How It Works 61

The Code 62

3-3. Set Default Route Values 63

Problem 63

Solution 63

How It Works 64

The Code 64

3-4. Set Optional Route Values 65

Problem 65

Solution 65

How It Works 65

The Code 66

3-5. Set Route Constraints 67

Problem 67

Solution 67

How It Works 67

The Code 69

3-6. Define Remote Procedure Call Style Routes 71

Problem 71

Solution 71

How It Works 72

The Code 73

3-7. Create Catch-all Routes 75

Problem 75

Solution : 75

How It Works 75

The Code 75

3-8. Prevent Controller Methods from Inadvertently Becoming Web API Endpoints 76

Problem 76

Solution 76

i CONTENTS

How It Works 76

Solution 78

3-9. Configure Route-Specific Message Handlers 78

Problem 78

Solution 78

How It Works 79

The Code 79

3-10. Ignore Routes 80

Problem 80

Solution 80

How It Works 81

The Code 82

3-11. Localize Routes 82

Problem 82

Solution 82

How It Works 83

The Code 84

3-12. Generate a Link to the Route 87

Problem 87

Solution 87

How It Works 88

The Code 88

Chapter 4: Content Negotiation and Media Types 91

4-1. Request a Specific Media Type from ASP.NET Web API 91

Problem 91

Solution 91

How It Works 91

The Code 92

4-2. Support Additional Media Types 94

Problem 94

Solution 94

xi

CONTENTS

How It Works 94

The Code 95

4-3. Control Model Binding From URI and Body 96

Problem 96

Solution 96

How It Works 96

The Code 97

4-4. ASP.NET MVC-style Parameter Bindingin ASP.NET Web API 100

Problem 100

Solution 101

How It Works 101

The Code 101

4-5. Customize XML and JSON Responses 102

Problem 102

Solution 102

How It Works 102

The Code 103

4-6. Write Your Own MediaTypeFormatter 104

Problem 104

Solution 104

How It Works 104

The Code 105

4-7. Run Content Negotiation Manually 109

Problem 109

Solution; 109

How It Works 109

The Code 110

4-8. Bypass Content Negotiation 111

Problem 111

Solution 111

xii

$ CONTENTS

How It Works 111

The Code 111

4-9. Control Media Types with MediaTypeMappings 114

Problem 114

Solution 114

How It Works 114

The Code 115

4-10. Serve Binary Data from ASP.NET Web API 116

Problem 116

Solution 116

How It Works 116

The Code 117

4-11. Accept File Uploads 119

Problem 119

Solution 119

How It Works 119

The Code 119

4-12 Enable Bufferless Uploads 122

Problem 122

Solution 122

How It Works 122

The Code 123

4- 13 Validate File Uploads 125

Problem 125

Solution 125

How It Works 126

The Code 126

Chapter 5: Configuration and Customization 129

5-1. Throttle ASP.NET Web API Calls 129

Problem 129

Solution 129

xiii

CONTENTS

How It Works 130

The Code 130

5-2. Use Controllers from an External Assembly 132

Problem 132

Solution 132

How It Works 132

The Code 133

5-3. Use Controller-Scoped Configuration 134

Problem 134

Solution 134

How It Works 134

The Code 135

5-4. Validate Input with Action Filters 137

Problem 137

Solution 137

How It Works 138

The Code 139

5-5. Override Filters 142

Problem 142

Solution 142

How It Works 142

The Code 142

5-6. Add Caching to ASP.NET Web API 143

Problem 143

Solution: 144

How It Works 144

The Code 145

5-7. Use an Existing Caching Library 147

Problem 147

Solution 147

xiv

V CONTENTS

How It Works 147

The Code 149

5-8. Order Filter Execution with Custom Filters 151

Problem 151

Solution 151

How It Works 151

The Code 152

5-9. Order Filter Execution Without Custom Filters 156

Problem 156

Solution 156

How It Works 156

The Code 156

5-10. Customize Error Detail Policy 158

Problem 158

Solution 158

How It Works 159

The Code 160

5-11. Return HTMLfromASP.NET Web API 161

Problem 161

Solution 161

How It Works 161

The Code 161

5-12. Store Objects for the Duration of HTTP Request 166

Problem 166

Solution 166

How It Works 166

The Code 168

Chapter 6: Embrace HTTP with ASP.NET Web API 171

6- 1. Work Directly with HttpRequestMessage 171

Problem 171

Solution 171

XV

CONTENTS

How It Works 172

The Code 173

6-2. Support Partial GET 175

Problem 175

Solution 175

How It Works 175

The Code 176

6-3. Support the HEAD Verb 178

Problem 178

Solution 178

How It Works 179

The Code 180

6-4. Support the PATCH Verb 182

Problem 182

Solution 182

How It Works 182

The Code 183

6-5. Support Batching of HTTP Requests 186

Problem 186

Solution 186

How It Works 187

The Code 188

6-6. Automatic HTTP 406/Not Acceptable Server Responses 189

Problem 189

Solution ; 189

How It Works 190

The Code 190

6-7. Implement Versioning of a Web API with Centralized Routes 190

Problem 190

Solution 191

xvi

S CONTENTS

How It Works 191

The Code 191

6-8. Implement Versioning of a Web API with Attribute Routing 197

Problem 197

Solution 198

How It Works 198

The Code 198

6-9. Use Custom HTTP Content 201

Problem 201

Solution 201

How It Works 202

The Code 202

Chapter 7: Exceptions, Troubleshooting, and Documenting 205

7-1. Handle Exceptions with Filters 205

Problem 205

Solution 205

How It Works 206

The Code 206

7-2. Handle Exceptions Globally 207

Problem 207

Solution 208

How It Works 208

The Code 209

7-3. Log Exceptions Globally 210

Problem 210

Solution 210

How It Works 210

The Code 211

7-4. Add a Tracer 213

Problem 213

Solution 213

xvii

CONTENTS

How It Works 214

The Code214

7-5. Use an External Tracer 215

Problem 215

Solution 215

How It Works 215

The Code 215

7-6. Call the Tracer Manually 216

Problem 216

Solution216

How It Works 217

The Code 217

7-7. Write a Real-Time Tracer 218

Problem 218

Solution 218

How It Works 219

The Code 219

7-8. Create a Documentation Page for ASP.NET Web API 223

Problem 223

Solution 223

How It Works 223

The Code 224

7-9. Add Custom Samples to ASP.NET Web API Help Page 225

Problem 225

Solution 225

How It Works 226

The Code 226

7-10. Add Filters Information to ASP.NET Web API Help Page 228

Problem 228

Solution 228

xviii

* CONTENTS

How It Works 229

The Code 229

7-11. Support Data Annotations in ASP.NET Web API Help Page 231

Problem 231

Solution 231

How It Works 231

The Code 232

Chapter 8: Cross Domain and Push Communication 235

8-1. Use JSONP in ASP.NET Web API 235

Problem 235

Solution 235

How It Works 236

The Code 237

8-2. Use CORS in ASP.NET Web API 239

Problem 239

Solution 239

How It Works 239

The Code 240

8-3. Create Custom CORS Policies 243

Problem 243

Solution 243

How It Works 243

The Code 244

8-4. Support Streaming and Push from ASP.NET Web API 247

Problem 247

Solution 248

How ItWorks 248

The Code 249

8-5. Support Server-Sent Events in ASP.NET Web API 251

Problem 251

Solution 251

xix

CONTENTS

How It Works 251

The Code 252

8-6. lntegrateASP.NET SignaIR into ASP.NET Web API controllers 255

Problem 255

Solution 256

How It Works 256

The Code 257

8-7. Use WebSockets with ASP.NET Web API 263

Problem 263

Solution 263

How It Works 264

The Code 265

Chapter 9: Dependency Injection 269

9- 1. Inject Dependencies into ASP.NET Web API Controllers 269

Problem 269

Solution 269

How It Works 269

The Code 270

9-2. Add Support for the Most Popular Dl Containers 272

Problem 272

Solution 272

How It Works 273

The Code 273

9-3. Deal with Request Scope 274

Problem '. 274

Solution 274

How It Works 274

The Code 275

9-4. Dl with OtherASP.NET Web API Components 275

Problem 275

Solution 276

xx

m CONTENTS

How It Works 276

The Code 276

9-5. Write a Custom Dl Adapter 278

Problem 278

Solution 278

How It Works 279

The Code 279

Chapter 10: Securing an ASP.NET Web API Service 283

10-1. Use Correct Web API Components for Security-Related Tasks 283

Problem 283

Solution 284

How It Works 284

The Code 286

10-2. Add HHPS Support to ASP.NET Web API 288

Problem 288

Solution 288

How It Works 289

The Code 290

10-3. Use Basic Authentication 292

Problem 292

Solution 292

How It Works 292

The Code 295

10-4. Integrate Windows Authentication 296

Problem 296

Solution 296

How It Works 297

The Code 299

10-5. Use the Hawk Authentication Scheme 301

Problem 301

Solution 301

xxi

CONTENTS

How It Works 301

The Code 302

10-6. Use OAuth 2.0 with ASP.NET Web API 303

Problem 303

Solution 303

How It Works 304

The Code 306

10-7. Safely Access Current IPrincipal 309

Problem 309

Solution 310

How It Works 310

The Code 312

10-8. Remove ASP.NET Web API Server Footprint 313

Problem 313

Solution 313

How It Works 313

The Code 314

Chapter 11: Testing Web API Services 317

11-1. Unit Test ASP.NET Web API Controllers 317

Problem 317

Solution 317

How It Works 318

The Code 318

11-2. Unit Test Message Handlers 321

Problem '. 321

Solution 321

How It Works 322

The Code 322

11-3. Unit Test Action Filters 323

Problem 323

Solution 323

xxii

S CONTENTS

How It Works 324

The Code 325

11-4. Unit Test Formatters 326

Problem 326

Solution 326

How It Works 327

The Code 327

11-5. Simplify Tests with IHttpActionResult 331

Problem 331

Solution 331

How It Works 331

The Code 332

11-6. Test Routes 333

Problem 333

Solution 333

How It Works 334

The Code 334

11 -7. Integration Testing 338

Problem 338

Solution 338

How It Works 338

The Code 339

11-8. Integration Testing with OWIN 343

Problem 343

Solution 343

How It Works 343

The Code 344

Chapter 12: OData 347

12-1. Creating OData Services in Web API 347

Problem 347

Solution 347

xxiii

: CONTENTS

How It Works 348

The Code 349

12-2. Manage OData Routes 352

Problem 352

Solution 352

How It Works 353

The Code 355

12-3. Enable OData Queries 356

Problem 356

Solution 356

How It Works 357

The Code 357

12-4. Support OData Functions and Actions 360

Problem 360

Solution 360

How It Works 360

The Code 361

Index 365

xxiv