Uri.QueryString.Composer 2.0.0

dotnet add package Uri.QueryString.Composer --version 2.0.0                
NuGet\Install-Package Uri.QueryString.Composer -Version 2.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="Uri.QueryString.Composer" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Uri.QueryString.Composer --version 2.0.0                
#r "nuget: Uri.QueryString.Composer, 2.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 Uri.QueryString.Composer as a Cake Addin
#addin nuget:?package=Uri.QueryString.Composer&version=2.0.0

// Install Uri.QueryString.Composer as a Cake Tool
#tool nuget:?package=Uri.QueryString.Composer&version=2.0.0                

Uri QueryString Composer

GitHub license PRs Welcome Coverage Status NuGet package NuGet downloads

Have you ever needed to make an http call and had to assemble a giant query string entirely manually?

I hope you never have to. 😆

What is it?

It's a simple library that allows you to turn a class into a query string for https calls.

Usage

The implementation is available through a static class QueryStringComposer.

The two available overloads perform the same conversion.

Global configuration

You can use the method Configure provided by QueryStringComposerConfiguration in your service configuration to change the key case style globally.

QueryStringComposerConfiguration.Configure(options =>
{
    options.KeyNameCaseStyle = StringCaseStyle.TrainCase;
});

There are some supported style cases, they are:

public enum StringCaseStyle
{
    CamelCase = 1,
    PascalCase = 2,
    SnakeCase = 3,
    KebabCase = 4,
    TrainCase = 5
}

String overload

Code result: http://localhost?SomeName=Victor&SomeAge=20

const string baseUrl = "http://localhost";

var queryObject = new YourClass
{
    SomeName = "Victor",
    SomaAge = 20
};

var result = QueryStringComposer.Compose(baseUrl, queryObject);

Uri overload

Code result: http://localhost?SomeName=Victor,Juan&SomeAge=20,21

var uri = new Uri("http://localhost");

// Some Uri Changes

var queryObject = new YourClass
{
    SomeNames = new List<string> { "Victor", "Juan" },
    SomaAges = new List<int> { 20, 21 }
};

var result = QueryStringComposer.Compose(uri, queryObject);

Attributes

QueryStringKeyNameAttribute

In case you need to pass a custom value as a key and don't want to mess up your code with non-standard names. You can use the QueryStringKeyNameAttribute attribute for this.

Code result: http://localhost?user_name=Jorge

const string baseUrl = "http://localhost";

var queryObject = new YourClass
{
    UserName = "Jorge",
};

var result = QueryStringComposer.Compose(uri, queryObject);

class YourClass
{
    [QueryStringKeyName("user_name")]
    public string UserName { get; set; }
}

QueryStringIgnoreAttribute

In case you only need to ignore one property and don't want to create a new class for it. You can use the QueryStringIgnoreAttribute attribute for this.

Code result: http://localhost?Login=victorvhn

const string baseUrl = "http://localhost";

var queryObject = new YourClass
{
    Login = "victorvhn",
    Password = "d74ff0ee8da3b9806b18c8"
};

var result = QueryStringComposer.Compose(uri, queryObject);

class YourClass
{
    public string Login { get; set; }

    [QueryStringIgnore]
    public string Password { get; set; }
}

QueryStringKeyCaseStyleAttribute

In case you need to change the key case style of a single property and don't want to change it globally. You can use the QueryStringKeyCaseStyleAttribute attribute for this.

Code result: http://localhost?Key-In-Train-Case=value

const string baseUrl = "http://localhost";

var queryObject = new YourClass
{
    KeyInTrainCase = "value",
};

var result = QueryStringComposer.Compose(uri, queryObject);

class YourClass
{
    [QueryStringKeyCaseStyleAttribute(StringCaseStyle.TrainCase)]
    public string KeyInTrainCase { get; set; }
}

Attention when using

Some types are not supported for conversion:

  • Complex Lists.
  • Complex Dictionaries.

Providing a dictionary to compose, the values will not be converted.

Code result: http://localhost?key1=value1&key2=value2

const string baseUrl = "http://localhost";

var dic = new Dictionary<string, string>
{
    { "key1", "value1" },
    { "key2", "value2" }
};

var result = QueryStringComposer.Compose(baseUrl, dic);

Package

Nuget

License

MIT

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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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
2.0.0 2,806 4/16/2023
1.0.1 5,369 3/14/2022
1.0.0 20,356 12/20/2021

It's a simple library that allows you to turn a class into a query string for https calls.