You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Explain how to set the HttpClient's BaseAddress #12610
In previous patterns, we would explicitly set the HttpClient's BaseAddress property. This makes the subsequent method calls a lot easier/simpler, by not having to concatenate URL string parts. You don't mention how we do that with this new factory pattern or if it's different at all.
In fact, your example shows a base address (_remoteServiceBaseUrl), but glosses over where/how it gets a value.
public class CatalogService : ICatalogService
{
private readonly HttpClient _httpClient;
private readonly string _remoteServiceBaseUrl;
public CatalogService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Catalog> GetCatalogItems(int page, int take,
int? brand, int? type)
{
var uri = API.Catalog.GetAllCatalogItems(_remoteServiceBaseUrl,
page, take, brand, type);
var responseString = await _httpClient.GetStringAsync(uri);
var catalog = JsonConvert.DeserializeObject<Catalog>(responseString);
return catalog;
}
}
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: 7b506236-b211-eb9d-b571-5efecdde76c8
Version Independent ID: 24434b0e-0b82-e477-9440-2cc8f135df75
Activity
[-]Explain where baseUrl comes from[/-][+]Explain how to set the HttpClient's BaseAddress[/+]AnthonyMastrean commentedon May 31, 2019
Actually, I see the explicit BaseAddress handling in another linked page: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1#typed-clients