0% found this document useful (1 vote)
1K views

Core 3 Web API ToC PDF

This document discusses various topics related to building web APIs with ASP.NET Core such as project configuration, logging, database modeling, handling HTTP requests and responses, validation, asynchronous code, filtering, sorting, and more. It provides code examples and explanations for implementing common features in web APIs step-by-step.

Uploaded by

Deimer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views

Core 3 Web API ToC PDF

This document discusses various topics related to building web APIs with ASP.NET Core such as project configuration, logging, database modeling, handling HTTP requests and responses, validation, asynchronous code, filtering, sorting, and more. It provides code examples and explanations for implementing common features in web APIs step-by-step.

Uploaded by

Deimer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. PROJECT CONFIGURATION ........................................................

1.1 Creating a New Project............................................................................. 1

1.2 launchSettings.json File Configuration ..................................................... 3

1.3 Program.cs and Startup.cs Explanations .................................................. 4

1.4 Extension Methods and CORS Configuration............................................. 6

1.5 IIS Configuration ..................................................................................... 7

1.6 Additional Code in the Startup Class ........................................................ 9

1.7 Environment-Based Settings .................................................................. 10

2. CONFIGURING A LOGGING SERVICE ........................................ 13

2.1 Creating the Required Projects .............................................................. 13

2.2 Creating the ILoggerManager Interface and Installing NLog ................. 14

2.3 Implementing the Interface and Nlog.Config File ................................... 16

2.4 Configuring Logger Service for Logging Messages ................................. 17

2.5 DI, IoC, and Logger Service Testing ....................................................... 19

3. DATABASE MODEL AND REPOSITORY PATTERN ....................... 21

3.1 Creating Models ..................................................................................... 21

3.2 Context Class and the Database Connection ........................................... 23

3.3 Migration and Initial Data Seed .............................................................. 25

3.4 Repository Pattern Logic ........................................................................ 28

3.5 Repository User Interfaces and Classes ................................................. 30


3.6 Creating a Repository Manager .............................................................. 32

4. HANDLING GET REQUESTS....................................................... 36

4.1 Controllers and Routing in WEB API ....................................................... 36

4.2 Naming Our Resources ........................................................................... 38

4.3 Getting All Companies From the Database ............................................. 39

4.4 Testing the Result with Postman ............................................................ 42

4.5 DTO Classes vs. Entity Model Classes ..................................................... 44

4.6 Using AutoMapper in ASP.NET Core ........................................................ 47

5. GLOBAL ERROR HANDLING ...................................................... 50

5.1 Handling Errors Globally with the Built-In Middleware........................... 50

5.2 Startup Class Modification ...................................................................... 51

5.3 Testing the Result .................................................................................. 52

6. GETTING ADDITIONAL RESOURCES ......................................... 54

6.1 Getting a Single Resource From the Database ........................................ 54

6.2 Parent/Child Relationships in Web API .................................................. 56

6.3 Getting a Single Employee for Company ................................................. 59

7. CONTENT NEGOTIATION .......................................................... 62

7.1 What Do We Get Out of the Box? ............................................................ 62

7.2 Changing the Default Configuration of Our Project ................................ 63

7.3 Testing Content Negotiation ................................................................... 64

7.4 Restricting Media Types ......................................................................... 64


7.5 More About Formatters .......................................................................... 65

7.6 Implementing a Custom Formatter ........................................................ 66

8. METHOD SAFETY AND METHOD IDEMPOTENCY ........................ 69

9. CREATING RESOURCES ............................................................ 71

9.1 Handling POST Requests ........................................................................ 71

9.2 Code Explanation ................................................................................... 73

9.3 Creating a Child Resource ...................................................................... 75

9.4 Creating Children Resources Together with a Parent ............................. 78

9.5 Creating a Collection of Resources ......................................................... 80

9.6 Model Binding in API .............................................................................. 83

10. WORKING WITH DELETE REQUESTS ...................................... 87

10.1 Deleting a Parent Resource with its Children ......................................... 88

11. WORKING WITH PUT REQUESTS ............................................ 91

11.1 Updating Employee ................................................................................ 91


11.1.1 About the Update Method from the RepositoryBase Class ........................ 94

11.2 Inserting Resources while Updating One ............................................... 95

12. WORKING WITH PATCH REQUESTS ....................................... 97

12.1 Applying PATCH to the Employee Entity ................................................. 98

13. VALIDATION ........................................................................ 104

13.1 Validation while Creating Resource ...................................................... 105


13.1.1 Validating Int Type ............................................................................109
13.2 Validation for PUT Requests ................................................................. 111

13.3 Validation for PATCH Requests ............................................................. 113

14. ASYNCHRONOUS CODE ........................................................ 117

14.1 What is Asynchronous Programming? .................................................. 117

14.2 Async, Await Keywords, and Return Types ........................................... 118


14.2.1 The IRepositoryBase Interface and the RepositoryBase Class Explanation .120

14.3 Modifying the ICompanyRepository Interface and the


CompanyRepository Class .............................................................................. 120

14.4 IRepositoryManager and RepositoryManager Changes......................... 121

14.5 Controller Modification ......................................................................... 122

15. ACTION FILTERS .................................................................. 126

15.1 Action Filters Implementation .............................................................. 126

15.2 The Scope of Action Filters ................................................................... 127

15.3 Order of Invocation .............................................................................. 128

15.4 Improving the Code with Action Filters ................................................ 130

15.5 Validation with Action Filters ............................................................... 130

15.6 Dependency Injection in Action Filters ................................................. 134

16. PAGING ............................................................................... 140

16.1 What is Paging? ................................................................................... 140

16.2 Paging Implementation ........................................................................ 141

16.3 Concrete Query .................................................................................... 143

16.4 Improving the Solution ........................................................................ 146


17. FILTERING ........................................................................... 150

17.1 What is Filtering? ................................................................................. 150

17.2 How is Filtering Different from Searching?........................................... 151

17.3 How to Implement Filtering in ASP.NET Core Web API ........................ 152

17.4 Sending and Testing a Query................................................................ 154

18. SEARCHING ......................................................................... 157

18.1 What is Searching?............................................................................... 157

18.2 Implementing Searching in Our Application ......................................... 157

18.3 Testing Our Implementation ................................................................ 159

19. SORTING ............................................................................. 162

19.1 What is Sorting?................................................................................... 162

19.2 How to Implement Sorting in ASP.NET Core Web API .......................... 164

19.3 Implementation – Step by Step ............................................................ 166

19.4 Testing Our Implementation ................................................................ 168

19.5 Improving the Sorting Functionality..................................................... 169

20. DATA SHAPING .................................................................... 172

20.1 What is Data Shaping? ......................................................................... 172

20.2 How to Implement Data Shaping ......................................................... 173

20.3 Step-by-Step Implementation .............................................................. 175

20.4 Resolving XML Serialization Problems .................................................. 179

21. SUPPORTING HATEOAS ....................................................... 182


21.1 What is HATEOAS and Why is it so Important?..................................... 182
21.1.1 Typical Response with HATEOAS Implemented ......................................183
21.1.2 What is a Link?..................................................................................183
21.1.3 Pros/Cons of Implementing HATEOAS ..................................................184

21.2 Adding Links in the Project .................................................................. 184

21.3 Additional Project Changes .................................................................. 187

21.4 Adding Custom Media Types ................................................................. 188


21.4.1 Registering Custom Media Types .........................................................189
21.4.2 Implementing a Media Type Validation Filter .........................................190

21.5 Implementing HATEOAS ....................................................................... 191

22. WORKING WITH OPTIONS AND HEAD REQUESTS ................ 197

22.1 OPTIONS HTTP Request ....................................................................... 197

22.2 OPTIONS Implementation .................................................................... 197

22.3 Head HTTP Request .............................................................................. 199

22.4 HEAD Implementation .......................................................................... 199

23. ROOT DOCUMENT................................................................. 201

23.1 Root Document Implementation .......................................................... 201

24. VERSIONING APIS ............................................................... 206

24.1 Required Package Installation and Configuration................................. 206

24.2 Versioning Examples ............................................................................ 208


24.2.1 Using Query String ............................................................................209
24.2.2 Using URL Versioning .........................................................................210
24.2.3 HTTP Header Versioning .....................................................................211
24.2.4 Deprecating Versions .........................................................................212
24.2.5 Using Conventions .............................................................................213
25. CACHING ............................................................................. 214

25.1 About Caching ...................................................................................... 214


25.1.1 Cache Types .....................................................................................214
25.1.2 Response Cache Attribute ...................................................................215

25.2 Adding Cache Headers.......................................................................... 215

25.3 Adding Cache-Store .............................................................................. 217

25.4 Expiration Model .................................................................................. 219

25.5 Validation Model................................................................................... 221

25.6 Supporting Validation........................................................................... 223


25.6.1 Configuration ....................................................................................224

25.7 Using ETag and Validation .................................................................... 226

26. RATE LIMITING AND THROTTLING ...................................... 230

26.1 Implementing Rate Limiting ................................................................. 230

27. JWT AND IDENTITY ............................................................. 234

27.1 Implementing Identity in ASP.NET Core Project................................... 234

27.2 Creating Tables and Inserting Roles..................................................... 236

27.3 User Creation ....................................................................................... 238

27.4 Big Picture ........................................................................................... 241

27.5 About JWT ............................................................................................ 242

27.6 JWT Configuration ................................................................................ 244

27.7 Protecting Endpoints ............................................................................ 246

27.8 Implementing Authentication .............................................................. 247

27.9 Role-Based Authorization ..................................................................... 253


28. DOCUMENTING API WITH SWAGGER ................................... 256

28.1 About Swagger ..................................................................................... 256

28.2 Swagger Integration Into Our Project.................................................. 257

28.3 Adding Authorization Support .............................................................. 261

28.4 Extending Swagger Configuration ........................................................ 264

29. DEPLOYMENT TO IIS ............................................................ 268

29.1 Creating Publish Files ........................................................................... 268

29.2 Windows Server Hosting Bundle .......................................................... 270

29.3 Installing IIS........................................................................................ 270

29.4 Configuring Environment File ............................................................... 273

29.5 Testing Deployed Application ............................................................... 275

You might also like