Skip to content

Add support for argument list surrogates in minimal APIs #40712

@davidfowl

Description

@davidfowl

Is 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 😄

Metadata

Metadata

Assignees

Labels

DocsThis issue tracks updating documentationapi-approvedAPI was approved in API review, it can be implementedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-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

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions