dotnet / dotnet/aspnetcore

Custom naming when binding nested objects as properties of a model

Open
#42,790 3 comments 1 reaction 0 assignees View on GitHub
area-mvc feature-model-binding
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I'd like to reopen issue #4935.

### Describe the solution you'd like

E.g. given a record for pagination parameters

```c#
record Pagination(
[FromQuery("page")] int Page,
[FromQuery("perPage")] int PerPage
);
```

Currently if I use composition for common query parameters like so

```c#
// Current behaviour
record GetUsersRequest(
[FromQuery]
Pagination Pagination
);
```

the framework will try to bind pagination parameters from query parameters `pagination.page` and `pagination.perPage`. I'd like to be able to change nested property prefix (`pagination` in that case) and the separator (dot).

```c#
// Desired behaviour
record GetUsersRequest(
[FromQuery(Name = "some-other-prefix", Separator = "-")]
Pagination Pagination
);
```

In that case the framework should try to bind the request from a query like using parameters `some-other-prefix-page` and `some-other-prefix-perPage`.

I also would like to be able to remove prefix and separator entirely basically flattening all the composition. It should probably require additional validation from the model binder to ensure unique names for all nested properties.

```c#
// Desired behaviour
record GetUsersRequest(
// Explicitly changed prefix and separator to empty strings
[FromQuery(Name = "", Separator = "")]
Pagination Pagination
);
```

which will lead to flat query structure `page` and `perPage`.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

The issue does not name files or tests. Start by tracing ASP.NET Core's model binder handling of nested [FromQuery] properties and review the earlier issue #4935. Done means supporting configurable nested prefixes and separators, including empty values for flattening, with validation for conflicting names.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.