Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 7 minimal API AsParametersAttribute not mapping my request model as expected

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.
like image 971
Jorvat Avatar asked Oct 27 '25 08:10

Jorvat


1 Answers

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.

like image 64
Guru Stron Avatar answered Oct 28 '25 21:10

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!