Skip to content

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

Open
@FatTigerWang

Description

@FatTigerWang

Is there an existing issue for this?

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

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

{
  "parameter": null
}

Looks like it's related to here

else if (modelBindingContext.ValueProvider.ContainsPrefix(parameter.Name))

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

{
  "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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions