4

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...

  • Using AsParametersAttribute I don't get any data in the model
  • Using FromBodyAttribute I get the data in the model as expected.
  • SignInRequest is a simple model which I've tried to convert to everything - class, record struct. It doesn't work with [AsParameters]. The IHttpRequest is just simplification of MediatR's IResult<TResponse> with my custom response model.
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Jorvat
  • 41
  • 3

1 Answers1

2

AsParametersAttribute is used to group handler parameters into 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 mediator {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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 2
    All right, I must've misunderstood. So there is no nice way to use it. I though I could call it from my frontend and just pass in `{ identifier: 'qwe', password: 'qwe' }`, but instead I would have to pass it in with another level of nesting - `{ data: { identifier: 'qwe', password: 'qwe' } }`. I must confess I feel a bit disappointed, but I was too carried away. I will revert my POST and PUT methods to use other attributes. Thank you for the answer. I will try to update the page with my solution once I figure it all out. – Jorvat Jan 01 '23 at 22:32
  • What does SignInRequest look like? – davidfowl Jan 05 '23 at 08:28