I'm new to Minimal API which is available in ASP.NET Core 6.0 and based on Microsoft's tutorials here and here, one can define a sample route for Get method like this:
app.MapGet("/", () => "Hello World!");
For the Post method, the following code is provided:
...
app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
db.Todos.Add(todo);
await db.SaveChangesAsync();
return Results.Created($"/todoitems/{todo.Id}", todo);
});
...
In other part of the overview, some Special types such as: HttpContext
, HttpRequest
, HttpResponse
, ... are introduced and it seems that they are injected as parameters to routing methods (Get, Post, ...); So all these parameters are available:
app.MapPost("/test", (HttpContext context, HttpRequest request, HttpResponse response) => "Hello world!");
My questions is: What other parameters are available here:
app.MapPost("/test", (**HERE???**) => "Hello World!") {};
CodePudding user response:
From the documentation parameter binding has next supported binding sources:
- Route values
- Query string
- Header
- Body (as JSON)
- Services provided by dependency injection
- Custom
And next special types (as you mentioned):
HttpContext
: The context which holds all the information about the current HTTP request or response.HttpRequest
: The HTTP requestHttpResponse
: The HTTP responseSystem.Threading.CancellationToken
: The cancellation token associated with the current http request.System.Security.Claims.ClaimsPrincipal
: The user associated with the request (HttpContext.User
).
Also you can use here types implementing custom binding methods:
TryParse
(to bind custom types for route, query, and header binding sources)
public static bool TryParse(string value, T out result);
public static bool TryParse(string value, IFormatProvider provider, T out result);
BindAsync
public static ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter);
public static ValueTask<T?> BindAsync(HttpContext context);
So basically you can have any parameter which can be resolved via DI (like TodoDb db
from the example) or is a special type (HttpContext
...) or can be bound in some way (from the request data (like Todo todo
from example will be bound from json request body) or by some custom magic).
CodePudding user response:
Route handlers are methods that execute when the route matches. Route handlers can be a function or any shape, including synchronous or asynchronous. Route handlers can be a lambda expression, a local function, an instance method or a static method.
This is from the documentation https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#request-handling. There is an example here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#request-handling, also here is an example of a repository in the lambda https://dev.to/moe23/getting-started-with-minimal-apis-in-net-6-4mi4 and another one using Func https://medium.com/executeautomation/understanding-and-working-with-asp-net-core-6-0-minimal-api-1d25fd9ecc95.