Skip to content

Commit

Permalink
add multiple parameter sources to readme (#55)
Browse files Browse the repository at this point in the history
* multiple parameter sources in Readme

fixes #53

* Update README.md

* update with code listing
  • Loading branch information
ilyanaDev authored Apr 2, 2021
1 parent 8d7769c commit c6239b0
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,59 @@ Examples of the configuration can be found in the sample API project

Below are what I expect will be some common questions:

### How do I use shared routing conventions
### How do I use shared routing conventions?

If you want to create a common route template for all or some subset of your Endpoints, simply create a BaseEndpoint of your own that inherits from `Ardalis.Api.Endpoints.BaseEndpoint` and add a `[Route]` attribute to it.

### Can I add more than one public routable method to an Endpoint class
### Can I add more than one public routable method to an Endpoint class?

Technically, yes. But **don't do that**. If you really want that, you should just use a Controller.

### How can I bind parameters from multiple locations to my model?

To do this, you'll need to decorate the properties of your model with the proper route attributes:

```
public class NewArticleRequest
{
[FromRoute(Name = "username")] public string Username { get; set; }
[FromRoute(Name ="category")] public string Category { get; set; }
[FromBody] public Article Article { get; set; }
}
```

Then, it's very important to include `[FromRoute]` in the method declaration in your endpoint using that model:

```
public override Task<ActionResult> HandleAsync([FromRoute] NewArticleRequest request)
```

Note the `[Route("/article")]` and `[HttpPost("{username}/{category}")]` lines below. These lines form the route string used in the `NewArticleRequest` class above.

```
[Route("/article")]
public class Post : BaseAsyncEndpoint
.WithRequest<NewArticleRequest>
.WithoutResponse
{
[HttpPost("{username}/{category}")]
[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
}
}
```

For more information, take a look at [this discussion](https://github.com/ardalis/ApiEndpoints/issues/42) and [this issue](https://github.com/ardalis/ApiEndpoints/pull/50). Thank you to @garywoodfine and @matt-lethargic.

## Roadmap

The following are some things I'd like to add to the project/package.
Expand Down Expand Up @@ -181,3 +226,4 @@ If you're using them or find one not in this list, feel free to add it here via
- [CleanArchitecture](https://github.com/ardalis/CleanArchitecture): A solution template for ASP.NET 3.x solutions using Clean Architecture.
- [PayrollProcessor](https://github.com/KyleMcMaster/payroll-processor): A smorgasbord of modern .NET tech written with functional and asynchronous patterns.
- [eShopOnWeb](https://github.com/dotnet-architecture/eShopOnWeb): Sample ASP.NET Core reference application, powered by Microsoft

0 comments on commit c6239b0

Please sign in to comment.