Add support for argument list surrogates in minimal APIs
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
The idea here is to support using a type that can act like a surrogate for the argument list in a minimal API. The motivation is about refactoring a minimal API that takes a large set of parameters into one that takes a single object with top level properties that represent what were once arguments. This is a common pattern seen with CQRS frameworks where a request object is made to bind all inputs.
Describe the solution you’d like
app.MapGet("/products/{id}", (int id, int pageSize, int page, ILogger<Program> logger, MyDb db) =>
{
logger.LogInformation("Getting products for page {Page}, page);
return db.Products.Skip((page - 1) * pageSize).Take(pageSize);
});
The user wishes to refactor this to:
app.MapGet("/products/{id}", ([Parameters]ProductRequest req) =>
{
req.Logger.LogInformation("Getting products for page {Page}, req.Page);
return req.Db.Products.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize);
});
public record ProductRequest(int Id, int PageSize, int Page, ILogger<Program> Logger, MyDb Db);
The parameter or type needs to be marked explicitly to identify that this is not from the body or elsewhere (query etc). These properties should also support binding attributes:
public class ProductRequest
{
[FromRoute]
public int Id { get; set; }
[FromQuery]
public int PageSize { get; set; }
[FromQuery]
public int Page { get; set; }
[FromServices]
public ILogger<Program> Logger { get; set; }
[FromServices]
public MyDb Db {get; set; }
}
This would only work for top level properties in this object.
Additional context
Open questions:
- Do we want to support multiple of these? No 😄
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:11 (11 by maintainers)
Top Results From Across the Web
Parameter binding in Minimal API applications
Parameter binding is the process of converting request data into strongly typed parameters that are expressed by route handlers.
Read more >API — Click Documentation (8.1.x)
All keyword arguments are forwarded to the underlying command class. For the params argument, any decorated params are appended to the end of...
Read more >How to use parameter binding in minimal APIs in ASP.NET ...
NET Core 7 is its support for parameter binding in minimal APIs. The goal of this post is to give you a head...
Read more >Using ASP.NET Core 7 Minimal APIs: Request Filters ... - InfoQ
Essentially, this service represents a TODO list where we can read, add, modify, and delete the items. The items are stored in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Some additional name ideas:
[AsParameters]
[Flatten]
[PropertiesAsParameters]
[ParameterList]
[Surrogate]
Issue for adding support to MVC: https://github.com/dotnet/aspnetcore/issues/42271