Skip to content

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

Closed
@davidfowl

Description

@davidfowl

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...

Metadata

Metadata

Assignees

Labels

Priority:2Work that is important, but not critical for the releasearea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labelstriage-focusAdd this label to flag the issue for focus at triage

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions