QueryUrlParams 0.1.4

dotnet add package QueryUrlParams --version 0.1.4
                    
NuGet\Install-Package QueryUrlParams -Version 0.1.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QueryUrlParams" Version="0.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryUrlParams" Version="0.1.4" />
                    
Directory.Packages.props
<PackageReference Include="QueryUrlParams" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add QueryUrlParams --version 0.1.4
                    
#r "nuget: QueryUrlParams, 0.1.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package QueryUrlParams@0.1.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=QueryUrlParams&version=0.1.4
                    
Install as a Cake Addin
#tool nuget:?package=QueryUrlParams&version=0.1.4
                    
Install as a Cake Tool

QueryUrlParams

NuGet Coverage Status Tests

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:

  1. 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_case if you want to keep original format in lower-case. Set SnakeCaseNameConvert to false.
  2. 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] - use Enum string representation as query parameter value (default numeric representation)
  3. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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