Breaking

Thursday, April 15, 2021

ASP.NET Core–Avoid AutoRedirect in named or typed httpclients

In ASP.NET Core I typically avoid to use the HttpClientFactory directly but instead I use the named or typed httpclients.

You can configure the client using the AddHttpClient method(in case of named clients):

or inside the constructur (in case of typed clients):

This provides proper encapsulation and isolation of the configuration of each httpclient.

But what if you want more control on the HttpClient behavior? For example, you want to disable AutoRedirect on the HttpClient. That is not possible through the configuration above.

In that case you can use the ConfigurePrimaryHttpMessageHandler extension method.

For a named client:

For a typed client:

No comments:

Post a Comment