Flurl - The elegant way to build URLs & set query params in .Net

I'm a software consultant who loves to build projects and share my learnings on this blog.
Search for a command to run...

I'm a software consultant who loves to build projects and share my learnings on this blog.
No comments yet. Be the first to comment.
Azure Functions is a powerful serverless compute platform, and Azure App Service handles a lot of the underlying configuration for you out of the box. However, there are scenarios where you may want t

If you’ve been exploring Azure AI learning paths or watching older demos, you’ve probably run into something confusing: The documentation and training links often reference Foundry (Classic) But the portal you’re using today might show Foundry (New...

In today's digital age, speed is paramount for web and mobile applications. Users expect instantaneous responses, and any delay can lead to a poor user experience and loss of engagement. Therefore, as developers, it's crucial to build web APIs that a...

Loops are fundamental constructs in any programming language. But have you ever wondered if there are any performance differences between them? For small collections, the difference might be negligible, but what about handling a million or 10 million...

When using an Azure Function app, it is necessary to connect it to a storage account, often referred to as Host storage. This storage is utilized by the Functions runtime, as well as by various triggers and bindings to coordinate between multiple run...

A common task when calling web apis or resources from code is building a URL and adding the necessary query string and parameters.
The most common, naive and frequent code that we see to achieve this is :
var userId = 1;
var completed = true;
var someParamValue = "abc";
Uri baseAddress = new Uri("https://jsonplaceholder.typicode.com/");
Uri apiUrl = new Uri($"/todos?userId={userId}&completed={completed}" +
$"&someParam={someParamValue}", UriKind.Relative);
And the typical issue with the above code is that you could accidentally miss the starting ? or the & and = in the query parameters and create bugs; another issue is the readability of the code. When there is a high number of query parameters in the url, the code tends to be messy and unclean.
Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library for .NET
Flurl adds extension methods to strings which helps us easily add path segment, set query params, set fragment, etc. in a clean and elegant manner.
Flurl can be installed via NuGet Package manager console as:
PM> Install-Package Flurl
Or via the NuGet Package manager UI:

Once installed, import the Flurl namespace and convert the messy URLs to more readable ones.
The initial code can now be modified and constructed with flurl as :
using Flurl;
string url = "https://jsonplaceholder.typicode.com/"
.AppendPathSegment("todos")
.SetQueryParam("userId", userId)
.SetQueryParam("completed", completed)
.SetQueryParam("someParam", someParamValue);
Apart from SetQueryParam there's also SetQueryParams extension that accepts object, any collection of key-value pairs, Tuples, or a Dictionary object.
Flurl extension methods are available on String as well on System.Uri. In either case, it's easy to convert back and forth using the ToString() and ToUri() methods.
using Flurl;
Uri url = "https://jsonplaceholder.typicode.com/"
.AppendPathSegment("todos")
.SetQueryParams(new
{
userId = userId,
completed = completed,
someParam = someParamValue
})
.ToUri();
Flurl provides lots of other utilities and extension methods too. Do check out flurl documentation for more details.