CSharp Http Message handler
Handlers kan brukes som middleware for å endre http requesten før den sendes.
Eksempel på å skrive om en URI:
public class CoolifyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request.RequestUri.ToString().Contains("whatever"))
{
request.RequestUri = new Uri(request.RequestUri.ToString().Replace("whatever", "whatevzz"));
}
return await base.SendAsync(request, cancellationToken);
}}
Startup.cs
services.AddTransient<CoolifyHandler>();
services.AddHttpClient<IRedditClient, RedditClient>(opt =>
{
opt.BaseAddress = new Uri(settings.ApiUrl);
})
.AddHttpMessageHandler<CoolifyHandler>();