I stumbled across the UriBuilder in C# that makes the code a little bit more readable instead of using string interpolation or concatenating multiple parts. For example I could do the following:
var uriBuilder = new UriBuilder("https://github.com/dotnet/aspnetcore/search");
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["q"] = "utf-8";
uriBuilder.Query = parameters.ToString();
Which would give me the url https://github.com/dotnet/aspnetcore/search?q=utf-8. This becomes especially handy, when dealing with multiple query parameters.
However, there is a limitation. It only alows me to build urls but I need it for only the path + query parameters. So I would like to build only the portion /dotnet/aspnetcore/search?q=utf-8 in some fancy way other than string interpolation or concatenation.
Let me explain you why I need this. In my services section, I have the following code:
services.AddHttpClient("GitHub", client =>
{
client.BaseAddress = new Uri("https://github.com/);
});
And now I can just do this in some service class:
private readonly HttpClient _httpClient;
public RequestService(IHttpClientFactory httpClientFactory)
{
_httpClient = httpClientFactory.CreateClient("GitHub");
}
When I send a request, the base address will already be set and I only need to define the path and the url parameters. Until now I haven't find a better way other than using string interpolation, which is a probably not the nicest way of doing.
public void SendRequest() {
var request = new HttpRequestMessage(HttpMethod.Get,
$"dotnet/aspnetcore/search?q={someString}");
var response = await client.SendAsync(request);
}