dotnet / dotnet/aspnetcore

Allow to bind records with both primary constructor and properties via AsParameters

Open
#62,226 0 comments 0 reactions 0 assignees View on GitHub
area-minimal feature-rdf feature-rdg
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

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

I'm adding pagination support to my app. I want to avoid adding limit/offset parameters to every request, so I've added base record type PagedRequest:
```csharp
public abstract record PagedReqeust
{
public int? Limit { get; init; }
public int? Offset { get; init; }
}
```

So I could use it like this:
```csharp
public record MyRequest(string MyParam1, string MyParam2) : PagedRequest;

app
.MapGet("/my-endpoint", ([AsParameters] MyRequest request) => { /* handle it */ });
```

Unfortunately, it doesn't work this way. `Limit` and `Offset` properties stay null even when query params provided.

### Describe the solution you'd like

Would be nice if `[AsParameters]` taken properties into account when primary constructor is present.

### Additional context

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