dotnet / dotnet/aspnetcore

Model binding with [FromQuery] fails when parameter name is 'filter' but succeeds when renamed

Open
#65,220 3 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

In the code below, the API fails to bind the filter parameter and just returns the default values (as seen in the attached screenshot). But if the action parameter is renamed to anything else, model binding works as expected.

Reproduction
```
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
// This does NOT work
[HttpGet]
public IActionResult Get([FromQuery] BaseSearchViewModel filter)
{
return Ok(filter);
}

// This works
[HttpGet]
[Route("Get2")]
public IActionResult Get2([FromQuery] BaseSearchViewModel renamed)
{
return Ok(renamed);
}
}
```

```
public class BaseSearchViewModel
{
public int PageIndex { get; init; } = 1;
public int PageTotal { get; set; }
public int PageSkip => (PageIndex - 1) * PageSize;
public int PageSize { get; init; } = 20;
public string? Filter { get; set; }
public string? Keyword { get; set; }
}

```
Observe that filter parameter in Get() returns default values (PageIndex = 1, Filter = null)
Observe that renamed parameter in Get2() correctly binds the query parameters

Image

Image

Image

Image

### Expected Behavior

The model should bind correctly regardless of whether the parameter is named filter or something else

Actual behavior:
Binding fails when the parameter name is filter. Renaming it fixes the issue.

@davidfowl

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

_No response_

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