Skip to content

QueryHelpers.AddQueryString should support array type parameters #7945

@CurlyBraceTT

Description

@CurlyBraceTT

Is your feature request related to a problem? Please describe.

I am trying to create complex queries from generic object, the opposite of what [fromQuery] attribute is doing related to class-types. QueryHelpers.AddQueryString would do, however I need to access private version with
IEnumerable<KeyValuePair<string, string>> queryString parameter, or a new version with Dictionary<string, StringValues> parameter.

https://github.com/aspnet/HttpAbstractions/blob/master/src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs#L63

To support array types like for queries like ids=1&ids=2&ids=3, because Dictionary version forces key uniqueness.

Describe the solution you'd like

public modification for the linked method or another way to create query from the object, including support the list of parameters like ids=1&ids=2&ids=3

Describe alternatives you've considered

Add each value with AddQueryString(string uri, string name, string value). I wonder how massive string concat will impact memory, also I think methods should support each other so I could easily use AddQueryString and ParseQuery together.

Additional context

Code context:

public static string AppendObjectToQueryString(string uri, object requestObject)
{
	var type = requestObject.GetType();
	var data = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
		.ToDictionary
		(
			p => p.Name,
			p => p.GetValue(requestObject)
		);

	foreach (var d in data)
	{
		if (d.Value == null)
		{
			continue;
		}

		if ((d.Value as string == null) && d.Value is IEnumerable enumerable)
		{
			foreach (var value in enumerable)
			{
				uri = QueryHelpers.AddQueryString(uri, d.Key, value.ToString());
			}
		}
		else
		{
			uri = QueryHelpers.AddQueryString(uri, d.Key, d.Value.ToString());
		}
	}

	return uri;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DoneThis issue has been fixedapi-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-http-abstractionsgood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions