Skip to main content

QueryStringGenerator by Tomi Parviainen

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: QueryStringGenerator

Package Description

Author: Tomi Parviainen

NuGet: https://www.nuget.org/packages/QueryStringGenerator/

You can find more details at https://github.com/tparviainen/query-string-generator

Source: https://github.com/tparviainen/query-string-generator

Original Readme

note

Query String Generator

C# incremental generator to create a method that returns the query string of the object.

Usage

1. Install the NuGet package

PM> Install-Package QueryStringGenerator

2. Update the Model(s)

Class must be decorated with QueryString attribute, which is declared in QueryStringGenerator namespace.

using QueryStringGenerator;

[QueryString]
public class Model
{
public int? Limit { get; set; }
public int? Offset { get; set; }
public string? Sort { get; set; }
}

3. Call ToQueryString Method to the Instance of the Class

By default the generated method name is ToQueryString, which when called returns the query string of the object.

var model = new Model
{
Limit = 10,
Sort = "Price"
};

Console.WriteLine($"Query string: {model.ToQueryString()}");

/*
This code example produces the following results:

Query string: &limit=10&sort=Price
*/

Generated Source Code

Below is the auto-generated extension method for the class defined in step 2. above.

// <auto-generated />

namespace QueryStringGenerator.App.Models
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("QueryStringGenerator", "1.0.0")]
public static class QueryStringExtensionForModel
{
public static string ToQueryString(this Model _this)
{
if (_this == null)
{
return string.Empty;
}

var sb = new global::System.Text.StringBuilder();

if (_this.Limit != null)
{
sb.Append($"&limit={_this.Limit}");
}

if (_this.Offset != null)
{
sb.Append($"&offset={_this.Offset}");
}

if (_this.Sort != null)
{
sb.Append($"&sort={System.Net.WebUtility.UrlEncode(_this.Sort)}");
}

return sb.ToString();
}
}
}

Supported Data Types

  • Nullable value types, including enums
  • Reference types

NOTE: The query string value for enum is the name of the enum starting with a lowercase character.

About

note

Generate from string properties of a class a query string for a URL.

How to use

Example (source csproj, source files)

This is the CSharp Project that references QueryStringGenerator

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="QueryStringGenerator" Version="1.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated />

namespace DemoQuery
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("QueryStringGenerator", "1.0.0")]
internal static class QueryStringExtensionForPerson
{
public static string ToQueryString(this Person _this)
{
if (_this == null)
{
return string.Empty;
}

var sb = new System.Text.StringBuilder();

if (_this.FirstName != null)
{
sb.Append($"&firstname={System.Net.WebUtility.UrlEncode(_this.FirstName)}");
}

if (_this.LastName != null)
{
sb.Append($"&lastname={System.Net.WebUtility.UrlEncode(_this.LastName)}");
}

return sb.ToString();
}
}
}

Useful

Download Example (.NET C#)

Share QueryStringGenerator

https://ignatandrei.github.io/RSCG_Examples/v2/docs/QueryStringGenerator

aaa

Category "EnhancementClass" has the following generators:

1 ApparatusAOT Nuget GitHub Repo stars 2023-04-16

2 AspectGenerator Nuget GitHub Repo stars 2024-01-07

3 CommonCodeGenerator Nuget GitHub Repo stars 2024-04-03

4 Comparison Nuget GitHub Repo stars 2025-05-25

5 DudNet Nuget GitHub Repo stars 2023-10-27

6 Enhanced.GetTypes Nuget GitHub Repo stars 2024-09-17

7 FastGenericNew Nuget GitHub Repo stars 2023-08-10

8 Immutype Nuget GitHub Repo stars 2023-08-12

9 Ling.Audit Nuget GitHub Repo stars 2023-12-12

10 Lombok.NET Nuget GitHub Repo stars 2023-04-16

11 M31.FluentAPI Nuget GitHub Repo stars 2023-08-25

12 MemberAccessor Nuget GitHub Repo stars 2025-03-24

13 MemoryPack Nuget GitHub Repo stars 2023-08-04

14 Meziantou.Polyfill Nuget GitHub Repo stars 2023-10-10

15 Microsoft.Extensions.Logging Nuget GitHub Repo stars 2023-04-16

16 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator Nuget GitHub Repo stars 2023-11-17

17 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16

18 OptionToStringGenerator Nuget GitHub Repo stars 2024-02-15

19 Program Nuget GitHub Repo stars 2025-11-06

20 QueryStringGenerator Nuget GitHub Repo stars 2024-11-07

21 RSCG_Decorator Nuget GitHub Repo stars 2023-09-30

22 RSCG_UtilityTypes NugetNuget GitHub Repo stars 2023-12-22

23 StaticReflection NugetNuget GitHub Repo stars 2023-10-13

24 SyncMethodGenerator Nuget GitHub Repo stars 2023-08-14

25 System.Runtime.InteropServices Nuget GitHub Repo stars 2023-04-16

26 System.Text.RegularExpressions Nuget GitHub Repo stars 2023-04-16

27 TelemetryLogging Nuget GitHub Repo stars 2023-11-30

28 ThisClass Nuget GitHub Repo stars 2024-04-19

See category

EnhancementClass