How to reliably build a URL in C# using the parts?
Check out the UriBuilder class
UriBuilder
is great for dealing with the bits at the front of the URL (like protocol), but offers nothing on the querystring side. Flurl [disclosure: I'm the author] attempts to fill that gap with some fluent goodness:
using Flurl;
var url = "http://www.some-api.com"
.AppendPathSegment("endpoint")
.SetQueryParams(new {
api_key = ConfigurationManager.AppSettings["SomeApiKey"],
max_results = 20,
q = "Don't worry, I'll get encoded!"
});
There's a new companion library that extends the fluent chain with HTTP client calls and includes some nifty testing features. The full package is available on NuGet:
PM> Install-Package Flurl.Http
or just the stand-alone URL builder:
PM> Install-Package Flurl