An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

## MatrixParameters.AspNetCore

[![CodeQL](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/codeql-analysis.yml)
[![Deploy](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/ci.yml/badge.svg)](https://github.com/EdwOK/MatrixParameters.AspNetCore/actions/workflows/ci.yml)
[![NuGet](https://img.shields.io/nuget/v/MatrixParameters.AspNetCore.svg)](https://www.nuget.org/packages/MatrixParameters.AspNetCore)
[![Downloads](https://img.shields.io/nuget/dt/MatrixParameters.AspNetCore.svg)](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);
}
```