The following is an example of how to implement a fake or mock backend in Blazor WebAssembly with an HTTP Client Handler.
A fake backend is used for doing backendless development in Blazor which allows you to run and test your code without a real backend api, it’s useful for developing front-end code without a backend or before the backend is available.
I created the below fake backend as part of a Blazor WebAssembly - User Registration and Login Example & Tutorial, it’s built by extending the Blazor HttpClientHandler
class and includes mock endpoints for authentication, registration and user management, and uses browser local storage to store data to mimic the behaviour of a real api with a database.
The fake backend contains a handleRoute()
local function that checks if the request matches one of the faked routes, matching requests are intercepted and handled by one of the below // route functions
, non-matching requests are sent through to the real backend by calling base.SendAsync(request, cancellationToken);
. Below the route functions there are // helper functions
for returning different response types and performing small tasks.
#blazor #webassembly #dotnet #programming #developer