dotnet / dotnet/aspnetcore

HttpGet [FromQuery] Model binding fails in case of propertity with the same name

Open
#57,637 6 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Model binding fails when a [FormQuery] parameter has the same name as a property of the model.
```cs
public class TestParameter
{
public string Parameter { get; set; };
}

[ApiController]
public class TestController()
{
[HttpGet("test")]
public TestParameter Test([FromQuery] TestParameter parameter)
{
return parameter;
}
}
```

*HTTP GET /test?Parameter=test*
```json
{
"parameter": null
}
```
Looks like it's related to here https://github.com/dotnet/aspnetcore/blob/c4601ad28a8b539bbfc696a4f70c4d8292cfe1e9/src/Mvc/Mvc.Core/src/ModelBinding/ParameterBinder.cs#L121

When the name of the model is the same as the model attribute name, the binding intention cannot be easily determined by the prefix

### Expected Behavior

*HTTP GET /test?Parameter=test*
```json
{
"parameter": "test"
}
```

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

9.0.100-preview.7.24407.12

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.