QueryStringGenerator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package QueryStringGenerator --version 1.0.0                
NuGet\Install-Package QueryStringGenerator -Version 1.0.0                
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="QueryStringGenerator" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add QueryStringGenerator --version 1.0.0                
#r "nuget: QueryStringGenerator, 1.0.0"                
#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.
// Install QueryStringGenerator as a Cake Addin
#addin nuget:?package=QueryStringGenerator&version=1.0.0

// Install QueryStringGenerator as a Cake Tool
#tool nuget:?package=QueryStringGenerator&version=1.0.0                

Query String Generator

C# source 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 code
using System;
using System.Net;
using System.Text;

namespace QueryStringGenerator.App.Models
{
    public static class QueryStringExtensionForModel
    {
        public static string ToQueryString(this Model _this)
        {
            if (_this == null)
            {
                return string.Empty;
            }

            var sb = new 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={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.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
1.2.0 81 11/25/2024
1.1.0 506 7/1/2023
1.0.0 805 7/7/2022