https://github.com/edwok/matrixparameters.aspnetcore
MatrixParameters.AspNetCore
https://github.com/edwok/matrixparameters.aspnetcore
aspnetcore charp dotnet matrix-parameters microsoft model-binding modelbinding open-api routes-api routing swagger swagger-api uri-scheme
Last synced: 3 months ago
JSON representation
MatrixParameters.AspNetCore
- Host: GitHub
- URL: https://github.com/edwok/matrixparameters.aspnetcore
- Owner: EdwOK
- License: mit
- Created: 2021-10-11T12:08:01.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-04T00:15:55.000Z (almost 2 years ago)
- Last Synced: 2025-08-24T17:31:49.758Z (5 months ago)
- Topics: aspnetcore, charp, dotnet, matrix-parameters, microsoft, model-binding, modelbinding, open-api, routes-api, routing, swagger, swagger-api, uri-scheme
- Language: C#
- Homepage:
- Size: 116 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
## MatrixParameters.AspNetCore
[](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/codeql-analysis.yml)
[](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/ci.yml)
[](https://www.nuget.org/packages/MatrixParameters.AspNetCore)
[](https://www.nuget.org/packages/MatrixParameters.AspNetCore/)
This library helps you to start using [matrix parameters (or matrix URIs)](http://www.w3.org/DesignIssues/MatrixURIs.html) in the route of API action.
## Code Example
### Startup
```csharp
services.AddControllers(options =>
{
options.ModelBinderProviders.Insert(0, new SegmentPrefixAttributeModelBinderProvider());
options.ModelBinderProviders.Insert(1, new MatrixParameterAttributeModelBinderProvider());
});
services.AddRouting(options =>
{
options.ConstraintMap.Add("SegmentPrefix", typeof(SegmentPrefixConstraint));
});
```
### Swagger
```csharp
services.AddSwaggerGen(c =>
{
c.ParameterFilter();
c.DocumentFilter();
});
```
### Controller
```csharp
// GET customers/2/bananas;color=yellow,green;rate=good/oregon
//
// This route with the segments {fruits} and {location} will match a path with two segments if they are not
// matched with the following two actions GetApplesFromWashington and GetApplesFromLocation. Both of their
// routes are more specific because of constraints, and thus matched prior to this.
[HttpGet]
[Route("{fruits}/{location}")]
public IActionResult GetFruitsFromLocation(
[SegmentPrefix] string fruits, // The fruits from the route segment {fruits}.
[MatrixParameter("bananas")] string[] color, // The matrix parameter color from the segment starting with "bananas". It is matched only if the fruits is "apples".
[SegmentPrefix] string location, // The location from the route segment {location}.
[MatrixParameter("{fruits}")] string[] rate) // The matrix parameter rate from the route segment "{fruits}".
{
var result = new Dictionary
{
{ "fruits", fruits },
{ "color of bananas", Join(color) },
{ "location", location },
{ "rate of " + fruits, Join(rate) }
};
return Ok(result);
}
```