Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding WithRequest that contains two inputs #50

Closed
wants to merge 1 commit into from

Conversation

matt-lethargic
Copy link

@matt-lethargic matt-lethargic commented Mar 9, 2021

This PR adds in the ability to specify two request parameters for an endpoint so that it is possible to have a route parameter and a body. The case I'm specifically talking about is where you need to POST/PUT/PATCH to a url /parents/123/children to create a child object.

This addresses what is talked about in issue #42 I believe this is a very common pattern and will solve issues for many people.

And if nothing else would it not be nice to solve 42!!!!

@garywoodfine
Copy link

garywoodfine commented Mar 12, 2021

@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<NewArticleRequest>
        .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<ActionResult> 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

@ardalis
Copy link
Owner

ardalis commented Mar 25, 2021

See discussion on #42.

Base automatically changed from master to main March 25, 2021 15:04
@matt-lethargic
Copy link
Author

As above see #42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants