ardalis / ardalis/ApiEndpoints
Allow for multiple parameters on an endpoint
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 232
- PR merge metrics
- No merged PRs in 30d
Description
Not sure if this is currently possible, but I've been trying to create a POST endpoint where part of the parameters come from the route. I can't get it to work with a single request parameter as the entire class is bound to the requets body.
For example, I'd like to be able to support scenarios like:
```
[Route("/Country/{id}/Rename")]
[HttpPost]
public override async Task HandleAsync(CountryIdentifier id, CountryRenameRequest request, CancellationToken cancellationToken = new CancellationToken())
{
}
```
This doen't seem to be currently possible if I use the `EndpointBasAsync.WithRequest.WithResponse` mechanism for declaring the endpoint.
I've worked around it by re-creating the `EndpointBaseAsync` structure and adapting it. Is this an appropriate thing to do, or is there a better way to achieve the same thing?
```
public static class EndpointAsync
{
public static class WithRequest
{
public abstract class WithResult : EndpointBase
{
public abstract Task HandleAsync(
TParam param,
TRequest request,
CancellationToken cancellationToken = default(CancellationToken)
);
}
public abstract class WithoutResult : EndpointBase
{
public abstract Task HandleAsync(
TParam param,
TRequest request,
CancellationToken cancellationToken = default(CancellationToken));
}
public abstract class WithActionResult : EndpointBase
{
public abstract Task> HandleAsync(
TParam param,
TRequest request,
CancellationToken cancellationToken = default(CancellationToken));
}
public abstract class WithActionResult : EndpointBase
{
public abstract Task HandleAsync(
TParam param,
TRequest request,
CancellationToken cancellationToken = default(CancellationToken));
}
public abstract class WithAsyncEnumerableResult : EndpointBase
{
public abstract IAsyncEnumerable HandleAsync(
TParam param,
TRequest request,
CancellationToken cancellationToken = default(CancellationToken));
}
}
// ... other code (truncated for clarity)
}
```
happy to submit a PR for this sort of thing :-)
Contributor guide
Research direction
Start with the EndpointBasAsync.WithRequest.WithResponse mechanism mentioned in the issue and compare it with the proposed EndpointAsync.WithRequest structure. Done means a POST endpoint can bind route parameters and a request body through the endpoint API without recreating the base hierarchy; the issue does not name specific files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100