HttpClientInterceptor
is not native to DotNet. Normally, you would inherit
HttpClientHandler
or
DelegatingHandler
to implement a
HttpClient
Interceptor that wraps the SendAsync method.
Are you using this 3rd party library:
GitHub - jsakamoto/Toolbelt.Blazor.HttpClientInterceptor: Intercept all of the sending HTTP requests on a client side Blazor application.[
^]. If yes, you need to raise a ticket and ask your question there.
UPDATE: Here is an example for you...
1. Server:
app.MapGet("/ok", () => Results.Ok());
app.MapGet("/bad_request", () => Results.BadRequest());
app.MapGet("/unauthorized", () => Results.Unauthorized());
2. Blazor Middleware:
public class MyHttpClientHandler : DelegatingHandler
{
private readonly NavigationManager navigationManager;
public MyHttpClientHandler(NavigationManager navigationManager)
{
this.navigationManager = navigationManager;
}
protected override async Task<HttpResponseMessage> SendAsync
(
HttpRequestMessage request,
CancellationToken cancellationToken
)
{
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
Console.WriteLine($"[MyHttpClientHandler]
{(int)response.StatusCode} -
{response.StatusCode}");
return response;
}
}
3. Blazor Index.razor page:
@page "/"
@inject HttpClient httpClient
<h1>Hello, world!</h1>
<button @onclick="OKRequest">OKRequest Test</button>
<button @onclick="BadRequest">BadRequest Test</button>
<button @onclick="Unauthorized">Unauthorized Test</button>
<p>@status</p>
@code
{
private string status = "--";
Task OKRequest() => ProcessRequest("/ok");
Task BadRequest() => ProcessRequest("/bad_request");
Task Unauthorized() => ProcessRequest("/unauthorized");
async Task ProcessRequest(string uri)
{
var result = await httpClient.GetAsync(uri);
status = $"{(int)result.StatusCode} - {result.StatusCode}";
}
}
4. Program.cs - register middleware
builder.Services.AddTransient<MyHttpClientHandler>();
builder.Services.AddHttpClient
(
"HttpClientInterceptor.ServerAPI",
client => client.BaseAddress = new
Uri(builder.HostEnvironment.BaseAddress)
)
.AddHttpMessageHandler<MyHttpClientHandler>();
When you run and click the buttons, watch either the Browser Developer Console window or VS Output window to see the console logging being produced by the
MyHttpClientHandler
Middleware.
Hope this helps!