© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
devhl

Middleware to modify url

I'm trying to modify my HttpClient so it modifies a url before sending. I think I see how to do it but I just want to check as I've never done this.

Also, what is a primary message handler? How is primary different than any other message handler?

                services.AddHttpClient("cocApi", config =>
                {
                    config.BaseAddress = new Uri(hostBuilder.Configuration["Settings:BaseAddress"]);
                })
                .ConfigurePrimaryHttpMessageHandler(sp => new SocketsHttpHandler()
                {
                    MaxConnectionsPerServer = 10
                })
                .AddHttpMessageHandler((services) => new Foo());
            });
}

public class Foo : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Console.WriteLine("hi"); // this fires so i guess i got it right
        return base.SendAsync(request, cancellationToken);
    }
}
                services.AddHttpClient("cocApi", config =>
                {
                    config.BaseAddress = new Uri(hostBuilder.Configuration["Settings:BaseAddress"]);
                })
                .ConfigurePrimaryHttpMessageHandler(sp => new SocketsHttpHandler()
                {
                    MaxConnectionsPerServer = 10
                })
                .AddHttpMessageHandler((services) => new Foo());
            });
}

public class Foo : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        Console.WriteLine("hi"); // this fires so i guess i got it right
        return base.SendAsync(request, cancellationToken);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Excel modify
C#CC# / help
2y ago
❔ string modify
C#CC# / help
3y ago
PDF modify lib
C#CC# / help
3y ago