Consuming Web APIs from Blazor WebAssembly
It’s time to feed our application and the page we have built with a real data source, let the app communicate with an online service to list the books available, and create the functionality for an admin to add new ones.
We will start this chapter by explaining why you need to communicate with an API from your Blazor app, in addition to understanding the built-in .NET class HttpClient, as well as other extensions that will be used to implement the communication with the API.
Next, we will practice sending GET
and POST
requests to our API. We will also explore some more advanced techniques to manage and manipulate HttpClient instances. We will use IHttpClientFactory and delegating handlers to apply global logic to HTTP requests and responses. By the end of the chapter, we will have learned how to encapsulate the API calls and take them out of the components to keep them clean and testable.
This chapter will enable you to consume...