I am trying to implement a simple generic minimal API MediatR method mapper (inspired by this video).
I ran into an issue regarding mapping my request model when receiving data from body in the POST method with AsParametersAttribute.
I've double checked in documentation that this is possible, but...
class, record struct. It doesn't work with [AsParameters]. The IHttpRequest is just simplification of MediatR's IResult<TResponse> with my custom response model.AsParametersAttribute is used to group handler parameters into a single data structure:
Specifies that a route handler delegate's parameter represents a structured parameter list.
I.e. if you have the following endpoint:
app.MapPost("sign-in", async (IMediator mediator, SignInRequest request) => ...);
This attribute will allow you to introduce a data structure to encapsulate those parameters:
public struct SignInHandlerParams
{
public IMediator Mediator {get; set;}
public SignInRequest Request {get; set;}
}
app.MapPost("sign-in", async ([AsParameters]SignInHandlerParams p ) => ...);
It does not replace the FromBody, FromServices etc. in any way.
Check out the parameter binding for argument lists with AsParameters doc for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With