3.57Configuring the Request Base Address
Created on Aug 17, 2026~4 min read
When integrating with multiple third-party APIs, we usually register and configure the BaseAddress of multiple HttpClient instances globally. For example:
// Configure the base address of the default clientservices.AddHttpClient(string.Empty, client =>{ client.BaseAddress = new Uri("https://furion.net/");});// Configure the base address of the GitHub clientservices.AddHttpClient("github", client =>{ client.BaseAddress = new Uri("https://github.com/");});You can then specify the client to use via the .SetHttpClientName(client name) method.
In addition to global configuration, the framework also supports locally setting the base address for a single request, allowing it to be specified dynamically when building the request:
// Set the base address using a stringHttpRequestBuilder.Get("/api/test") .SetBaseAddress("https://furion.net");// Set the base address using a Uri objectHttpRequestBuilder.Get("/api/test") .SetBaseAddress(new Uri("https://furion.net"));// Can be used as a prefixHttpRequestBuilder.Get("/api/test") .SetBaseAddress("/furion");Processing logic:
- If the request address is an absolute address, the request is sent directly using that address.
- If the request address is a relative address:
- When no local
BaseAddressis set, it is concatenated with theBaseAddressof the globalHttpClientinstance as the final request address. - When a local
BaseAddressis set:- If the local
BaseAddressis a relative address, the localBaseAddressis first concatenated before the request address, and then concatenated with the globalBaseAddress. - If the local
BaseAddressis an absolute address, that absolute address is concatenated directly with the request address as the final request address (the globalBaseAddressis ignored in this case).
- If the local
- When no local