Skip to content

InferParameterBindingInfoConvention ignores property's binding source attributes #46140

@brunolins16

Description

@brunolins16

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

ASP.NET Core infers the parameter binding source for all API controllers following this rule:

  1. A previously specified BindingInfo.BindingSource is never overwritten.
  2. A complex type parameter, registered in the DI container, is assigned BindingSource.Services.
  3. A complex type parameter, not registered in the DI container, is assigned BindingSource.Body.
  4. Parameter with a name that appears as a route value in ANY route template is assigned BindingSource.Path.
  5. All other parameters are BindingSource.Query.

However, when the parameter type includes properties with binding source attributes the inferred source, when no attribute specified in the parameter, will be BindingSource.Body breaking the expected behavior, since all properties' attributes will be ignored as described in https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0#frombody-attribute

Expected Behavior

Is expected in this case that the source is not inferred and let the binding mechanism identify based on the property's attributes.

Steps To Reproduce

[HttpPost]
public IActionResult SomeAction(Model model)
{
    return Ok(model);
}

public record Model
{
    [FromRoute]
    public RouteModel RouteModel { get; init; }

    [FromQuery]
    public QueryModel QueryModel { get; init; }
    
    [FromBody]
    public BodyModel BodyModel { get; init; }
}

Exceptions (if any)

N/A

.NET Version

7.0

Anything else?

Related: #45392

Metadata

Metadata

Assignees

Labels

old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions