The Wayback Machine - https://web.archive.org/web/20210920192417/https://github.com/dotnet/aspnetcore/issues/36726
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal APIs doesn't support binding to StringValues from query or headers #36726

Open
davidfowl opened this issue Sep 19, 2021 · 0 comments
Open

Comments

@davidfowl
Copy link
Contributor

@davidfowl davidfowl commented Sep 19, 2021

Describe the bug

Parameter binding doesn't support StringValues (which is ASP.NET Core's native representation of both query string and header values).

To Reproduce

using Microsoft.Extensions.Primitives;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/", (StringValues query) => query.ToString());

app.Run();

OR

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/", ([FromQuery] StringValues query) => query.ToString());

app.Run();

Exceptions (if any)

The first fails parameter binding things that StringValues is a body parameter.

Unhandled exception. System.InvalidOperationException: Body was inferred but the method does not allow inferred body parameters.
Below is the list of parameters that we found:

Parameter           | Source
---------------------------------------------------------------------------------
query               | Body (Inferred)

Did you mean to register the "Body (Inferred)" parameter(s) as a Service or apply the [FromService] or [FromBody] attribute?

When adding [FromQuery], it also fails but it's because StringValues doesn't have a TryParse method:

Unhandled exception. System.InvalidOperationException: No public static bool StringValues.TryParse(string, out StringValues) method found for query.

It feels bad that our native representation doesn't work...

@davidfowl davidfowl changed the title We don Minimal APIs doesn't support binding to StringValues from query or headers Sep 19, 2021
@davidfowl davidfowl added this to Ready in Minimal APIs 6.0 via automation Sep 19, 2021
@rafikiassumani-msft rafikiassumani-msft moved this from Ready to Need review in Minimal APIs 6.0 Sep 20, 2021
@rafikiassumani-msft rafikiassumani-msft removed this from Need review in Minimal APIs 6.0 Sep 20, 2021
@rafikiassumani-msft rafikiassumani-msft added this to the .NET 7 Planning milestone Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants