C# Flurl an elegant, fluent tool for generating a complex URL

When we use HttpCient to call web api and its url, we often use string method to concatenate url’s parameter such as its id value. The example below shows one of the string methods we often use to concatenate url’s parameters.

The result console will show:

We can use either string.format method or C# 6 to set url’s parameters. However, these methods will result in one key problem – readability. You may have noticed when there are more parameters we have to set, the messier we will get for generating a complete url. One good thing is that there is an effective URL generator- Flurl, which will be introduced in this article.

Flurl can be downloaded by nuget

PM> Install-Package Flurl

We can also use dotnet cli to download this library

dotnet add package Flurl

After we download this library. We can simplify the above code as shown below

The above code use AppendPathSegment and SetQueryParam methods from Flurl library to construct a complete url by combining its url’s main path and its parameters.

          We can also use SetQueryParams method. This method is acceptable to object as well as any other key-value data structure types such as dict and tuples.

I believe that it is beneficial to learn and use this library by effectively using this library when generating a complex url. This will improve the code readability.