Closed
Description
Describe the bug
When using the ASPP.NET 6 minimal hosting, querystring binding does not work with arrays/lists
To Reproduce
app.MapGet("/qs/{id:int}", HttpHandlers.GetByQueryString);
public static string GetByQueryString(int id, string name, int[] values)
{
return id + " " + name + string.Join(",", values);
}
Adding [FromQuery(Name = "values")]
to the method signature does not help either.
Exceptions (if any)
An exception is thrown when sending in a request:
curl http://localhost:5000/qs/123\?name\=dave\&values\=1\&values\=2
Unhandled exception. System.InvalidOperationException: No public static bool Int32[].TryParse(string, out Int32[]) method found for values.
Further technical details
- ASP.NET Core version 6
.NET SDK (reflecting any global.json):
Version: 6.0.100-preview.5.21252.2
Commit: 921395c33e
Runtime Environment:
OS Name: Mac OS X
OS Version: 11.3
OS Platform: Darwin
RID: osx.11.0-x64
Base Path: /Users/jonathan/.dotnet/sdk/6.0.100-preview.5.21252.2/
Host (useful for support):
Version: 6.0.0-preview.5.21251.4
Commit: d2fba8fdc4
.NET SDKs installed:
2.1.809 [/Users/jonathan/.dotnet/sdk]
3.1.401 [/Users/jonathan/.dotnet/sdk]
3.1.403 [/Users/jonathan/.dotnet/sdk]
5.0.103 [/Users/jonathan/.dotnet/sdk]
6.0.100-preview.5.21229.26 [/Users/jonathan/.dotnet/sdk]
6.0.100-preview.5.21252.2 [/Users/jonathan/.dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.21 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.21 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.7 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.9 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.3 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0-preview.5.21229.9 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0-preview.5.21230.5 [/Users/jonathan/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.21 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.7 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.9 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.3 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0-preview.5.21229.1 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0-preview.5.21251.4 [/Users/jonathan/.dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
Metadata
Metadata
Assignees
Labels
This issue tracks updating documentationWork that is important, but not critical for the releaseIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcThis issue represents an ask for new feature or an enhancement to an existing oneController-like actions for endpoint routing*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Activity
[-]Minimal Hosting - array support in method signatures[/-][+]Minimal Hosting - array support in method signatures from querystring binding[/+]davidfowl commentedon May 8, 2021
Would this only be for simple types? Today we don't support any complex types for model binding and it conflicts with this idea
jchannon commentedon May 8, 2021
Yup simple types only imo, it's a query string, you shouldn't be doing crazy stuff like json binding. I mean there's nothing stopping someone adding json in a querystring I just don't think the framework should support that
[-]Minimal Hosting - array support in method signatures from querystring binding[/-][+]Minimal Actions - array support in method signatures from querystring binding[/+]ghost commentedon May 10, 2021
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
halter73 commentedon Jul 8, 2021
What collection types should we support?
What should we do if we know the param comes from the route? Fail at startup? Provide a single-element array?
DamianEdwards commentedon Aug 23, 2021
With the newly added support for
BindAsync(HttpContext)
on the target parameter type it would be easy to support this in the app or a library via a custom type, e.g.MultiValueQuery values
, whereMultiValueQuery
has aBindAsync
method on it.15 remaining items
davidfowl commentedon Feb 3, 2022
@jchannon This is supported in .NET 7 preview1
davidfowl commentedon Feb 4, 2022
I mean preview2
legistek commentedon Feb 23, 2022
Previously (in Framework) you could serialize an array of primitives to a JSON object in the query string like this:
?values=[1,2,3,4,5]
(escaped obviously)
and the method:
DoSomething(int[] values)
would bind correctly. That doesn't appear to be the case in .NET6 (or possibly earlier). I assume that was a known breaking change but I thought it worth asking? If it was not an intentional change then it's a different bug I guess.
davidfowl commentedon Feb 23, 2022
This issue isn't about MVC's binding semantics and minimal API supports a subset of what MVC supports. If you want to see what is supported for MVC today look at https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#collections
legistek commentedon Feb 23, 2022
Thanks. Sorry this seemed like the closest issue to what I'm experiencing that wasn't locked by the bots.
davidfowl commentedon Feb 23, 2022
😄. FWIW, it's not supported in MVC either, that's by design.