Fluent, immutable URL construction with path segments, query parameters, and proper encoding.
dotnet add package Philiprehberger.UrlBuilderFluent, immutable URL construction with path segments, query parameters, and proper encoding.
dotnet add package Philiprehberger.UrlBuilder
using Philiprehberger.UrlBuilder;
string url = Url.Base("https://api.example.com")
.Path("v2", "users")
.Query("page", 1)
.Query("sort", "name")
.Build();
// "https://api.example.com/v2/users?page=1&sort=name"
using Philiprehberger.UrlBuilder;
string url = Url.Base("https://example.com/search")
.Query("tag", new[] { "csharp", "dotnet" })
.RemoveQuery("obsolete")
.Build();
// "https://example.com/search?tag=csharp&tag=dotnet"
using Philiprehberger.UrlBuilder;
Uri uri = Url.Base("https://example.com")
.Path("docs")
.Fragment("getting-started")
.Port(8443)
.ToUri();
// https://example.com:8443/docs#getting-started
| Method | Description |
|---|---|
Base(string baseUrl) | Create a new UrlBuilder from a base URL. |
| Method | Description |
|---|---|
Path(params string[] segments) | Append path segments. |
Query(string key, object value) | Add a query parameter. |
Query(string key, IEnumerable values) | Add multiple values for a query parameter. |
RemoveQuery(string key) | Remove a query parameter by key. |
Fragment(string fragment) | Set the URL fragment. |
Scheme(string scheme) | Set the URL scheme. |
Host(string host) | Set the URL host. |
Port(int port) | Set the URL port. |
Build() | Build the URL as a string. |
ToUri() | Build the URL as a Uri instance. |
All methods return a new UrlBuilder instance, preserving immutability.
dotnet build src/Philiprehberger.UrlBuilder.csproj --configuration Release
If you find this project useful: