Skip to content

Minimal API query & route parameter mapping to an object #35304

@mumby0168

Description

@mumby0168

Summary

The minimal API's currently look for a public static bool TryParse(...) method to map parameters from query or route parameters to an object. It would be great to have this look at the properties on an object to match route and/or route parameters.

Motivation and goals

A get request on a minimal API route that takes a set of parameters where it may be nicer to map these to an object rather than a set of arguments on a delegate.

In scope

  1. Map query parameters to properties on an object for get & delete requests.
  2. Map route parameters to properties on an object for get & delete requests.

Out of scope

  • Supporting post & put requests.

Risks / unknowns

  • It may add more complexity to the by design set of minimal apis.

Examples

Query String Match

app.MapGet("api/todo", (FetchTodosQuery query) => {
    return Results.Ok($"Page: {query.Page} and size {query.PageSize}");
});

public class FetchTodosQuery
{
    public int Page { get; set; }

    public int PageSize { get; set; }
}

The above route would map from a query string such as this http://localhost:5001/api/todo?page=1&pageSize=10

Route Match

app.MapGet("api/todo/{page}/{pageSize}", (FetchTodosQuery query) => {
    return Results.Ok($"Page: {query.Page} and size {query.PageSize}");
});

public class FetchTodosQuery
{
    public int Page { get; set; }

    public int PageSize { get; set; }
}

The above route would map from a query string such as this http://localhost:5001/api/todo/1/10

Implicit vs Explicit

I would expect the choice of which method to use could always be overriden using the attributes [FromRoute] & [FromQuery].

Detailed design

I am pretty sure this is already supported in controllers for aspnet core when using the [FromQuery] attribute it will build up a dictionary of values from the query string and map these where possible to properties on an object.

I would expect this to work the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcdesign-proposalThis issue represents a design proposal for a different issue, linked in the descriptionfeature-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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions