In a .NET 6 Web API, in program.cs I have this:
The baseAddress content is : https://localhost:7122/ (coming from config file, baseAddress value is ok)
builder.Services.AddHttpClient<IMyService, MyService>(client => client.BaseAddress = new Uri(baseAddress));
When I execute the code below I get an error "'Invalid URI: The format of the URI could not be determined.' when I create the object HttpRequestMessage. I need to send an object when I do _httpClient.SendAsync
In MyService class
public async Task<MyResponse> Search(MyModel myModel)
{
var httpRequest = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("MyAction/Get"),
Content =
new StringContent(
JsonSerializer.Serialize(myModel),
Encoding.UTF8,
MediaTypeNames.Application.Json),
};
var response = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content
.ReadFromJsonAsync<MyResponse>()
.ConfigureAwait(false);
return responseBody;
}
When I replace RequestUri by this : RequestUri = new Uri($"https://localhost:7122/MyAction/Get") that's work.
Am I missed something ?