Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri Builder for Path only

Tags:

c#

httpclient

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);
}
like image 455
Tom el Safadi Avatar asked Dec 06 '25 15:12

Tom el Safadi


1 Answers

In order to properly init the path to includes its query params you can use the built-in QueryHelpers's AddQueryString extension methods (docs):

public void SendRequest() {
    Dictionary<string, string> queryString = new Dictionary<string, string>
    {
        { "q", "utf-8" }
    };
    string methodName = "dotnet/aspnetcore/search";
    methodName = QueryHelpers.AddQueryString(methodName, queryString);
    //methodName is "dotnet/aspnetcore/search?q=utf-8"

    var request = new HttpRequestMessage(HttpMethod.Get, methodName);
    var response = await this._httpClient.SendAsync(request);
}

Do not forget to add a using Microsoft.AspNetCore.WebUtilities;.

A little extra, the HttpRequestMessage has two parameterize constructors:

public HttpRequestMessage(HttpMethod method, string requestUri);
public HttpRequestMessage(HttpMethod method, Uri requestUri)

In order to work with the second constructor, you can easily use:

Uri uri = new Uri(methodName, UriKind.Relative);
var request = new HttpRequestMessage(HttpMethod.Get, uri);
like image 95
Shahar Shokrani Avatar answered Dec 08 '25 05:12

Shahar Shokrani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!