QueryUrlParams 0.1.4
dotnet add package QueryUrlParams --version 0.1.4
NuGet\Install-Package QueryUrlParams -Version 0.1.4
<PackageReference Include="QueryUrlParams" Version="0.1.4" />
<PackageVersion Include="QueryUrlParams" Version="0.1.4" />
<PackageReference Include="QueryUrlParams" />
paket add QueryUrlParams --version 0.1.4
#r "nuget: QueryUrlParams, 0.1.4"
#:package QueryUrlParams@0.1.4
#addin nuget:?package=QueryUrlParams&version=0.1.4
#tool nuget:?package=QueryUrlParams&version=0.1.4
QueryUrlParams
QueryUrlParams is a C# Source Generator high-performance source generator for building query URLs from DTO objects in C#.
This library generates ToQueryString() extension methods for your DTO classes, at compile time, to efficiently convert your decorated DTO classes into URL query strings, avoiding reflection overhead. You control the output using a set of custom attributes.
β¨ Features
- Compile-time generation of
ToQueryString()extension methods. - Custom attributes for fine-grained control over serialization.
- Simple, clear syntax and predictable output.
- No reflection β high performance at runtime.
- Supports:
- Nullable types
- Dates and booleans
- Nested decoreted DTOs
- Arrays and collections (e.g.,
List<T>,T[]) - Enums
- Custom objects (using ToString() method)
π Usage
Follow these steps to get started:
- Mark your DTO class with the
[GenerateQueryUrl]attribute
This enables the source generator to process your class and generate the extension method. You can optionally specify parameters to customize the behavior, for example:- Set a Base URL for the generated method, so you donβt need to pass it explicitly every time.
- Disable automatic conversion of property names to
snake_caseif you want to keep original format in lower-case. Set SnakeCaseNameConvert tofalse.
- Customize properties with attributes (optional)
[QueryParameterIgnore]- exclude a property from query parameters[QueryParameterName("customName")]- specify a custom key for a property[DateTimeFormat("yyyy-MM-dd")]- format DateTime properties[EnumAsString]- useEnumstring representation as query parameter value (default numeric representation)
- Call
.ToQueryUrl(baseUrl)on your DTO instance
This returns the full URL with all non-null properties serialized as query parameters with passing Base Url or without it.
π οΈ Example
using QueryUrlParams;
[GenerateQueryUrl]
public class SomeUrlParams
{
public string? Name { get; set; }
public int? Age { get; set; }
[QueryParameterIgnore] // This property will be ignored
public string? InternalToken { get; set; }
[QueryParameterName("phone")] // Use a custom key name in the query string
public string? PhoneNumber { get; set; }
[DateTimeFormat("yyyy-MM-dd")] // Format DateTime to only include date
public DateTime? StartDate { get; set; }
public List<string>? Tags { get; set; }
public Dictionary<string, string>? Metadata { get; set; }
public bool? IsActive { get; set; }
}
var dto = new SomeUrlParams
{
Name = "Alice",
Age = 28,
InternalToken = "secret", // Will NOT appear in URL
PhoneNumber = "+1234567890",
StartDate = new DateTime(2024, 5, 22),
Tags = new List<string> { "admin", "tester" },
Metadata = new Dictionary<string, string>
{
{ "ref", "email" },
{ "campaign", "spring2024" }
},
IsActive = true
};
string baseUrl = "https://example.com/api";
string url = dto.ToQueryUrl(baseUrl);
// Result: https://example.com/api/?name=Alice&age=28&phone=%2B1234567890&start_date=2024-05-22&tags=admin&tags=tester&ref=email&campaign=spring2024&is_active=true
π Performance Benchmark
QueryUrlParams shows significant performance improvements compared to traditional reflection-based URL generation. | Method | Mean | Error | StdDev | Gen0 | Allocated | |--------------------------- |---------:|----------:|----------:|-------:|----------:| | GenerateQueryUrl | 1.513 us | 0.0300 us | 0.0718 us | 0.5449 | 2.23 KB | | GenerateQueryUrlReflection | 5.200 us | 0.1020 us | 0.1990 us | 0.8672 | 3.57 KB |
This benchmark was measured by executing 1000 URL generations per method per iteration, capturing the total time for all and then averaging. The results demonstrate that source-generated code is about 3.5x faster and allocates significantly less memory then reflection-based URL generation.
π License
MIT License β feel free to use and contribute!
π Contributing
Contributions and feedback are welcome. Please open issues βοΈ
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Text.Encodings.Web (>= 9.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.4 | 204 | 5/28/2025 |