question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Minimal API query & route parameter mapping to an object

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Aug 20, 2021
0reactions
mumby0168commented, Aug 18, 2021

we’ve added minimal support for a TryParse overload that can work on custom types that takes the HttpContext (#35433). This should be enough of a stop gap to experiment with binding for custom types. We plan to build something more complete in .NET 7 based on feedback.

Okay that is great thank you, certainly will let me move forward with my idea. Will this issue continue to track the changes that will come as part of .NET 7 or will I need to keep my 👁️ out?

Read more comments on GitHub >

github_iconTop 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 >
How to use parameter binding in minimal APIs in ASP.NET ...
Minimal APIs in ASP.NET Core 7 offer several types of parameter binding including FromQuery, FromRoute, FromHeader, and FromBody.
Read more >
Using Query String Parameters with Minimal APIs
Handling Complex Query String Parameters With Minimal APIs. Now, let's try to map the query parameters into a class named SearchCriteria :.
Read more >
net 7 Route Handling and Parameter Binding in minimal apis
In .net 7, using the minimal apis is there a way to map multiple key-value pairs that appear in the url query parameters?...
Read more >
Using ASP.NET Core 7 Minimal APIs: Request Filters ... - InfoQ
Another useful feature of Minimal APIs is the ability to automatically map request parameters from the query string or the headers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found