Adding WithRequest that contains two inputs by matt-lethargic · Pull Request #50 · ardalis/ApiEndpoints (original) (raw)
@ardalis This PR would be really useful to have included.
EDIT : after further exploration there is no need for the PR. After exploring and fiddling around I realise that the Model Binding actually takes care of this anyway.
All that is needed is to create your Model Class and decorate your properties with the attributes you need. i.e.
public class NewArticleRequest { [FromRoute(Name = "author")] public string Author { get; set; }
[FromBody] public Article Article { get; set; }
}Then you can make use of it in your End Point as follows
[Route("/article")] public class Post : BaseAsyncEndpoint .WithRequest .WithoutResponse { [HttpPost("{author}")] [SwaggerOperation( Summary = "Submit a new article", Description = "Enables the submission of new articles", OperationId = "B349A6C4-1198-4B53-B9BE-85232E06F16E", Tags = new[] {"Article"}) ] public override Task HandleAsync([FromRoute] NewArticleRequest request, CancellationToken cancellationToken = new CancellationToken()) { //// your implementation } }
The only snag/issue/bug I seem to run into seems to be related only to the [FromRoute] is that it doesn't seem to bind those specific properties unless you explicity set the in the HandleAsync([FromRoute] NewArticleRequest request but other than that it seems to work