ArgumentativeFilters 0.0.1-preview9
See the version list below for details.
dotnet add package ArgumentativeFilters --version 0.0.1-preview9
NuGet\Install-Package ArgumentativeFilters -Version 0.0.1-preview9
<PackageReference Include="ArgumentativeFilters" Version="0.0.1-preview9" />
<PackageVersion Include="ArgumentativeFilters" Version="0.0.1-preview9" />
<PackageReference Include="ArgumentativeFilters" />
paket add ArgumentativeFilters --version 0.0.1-preview9
#r "nuget: ArgumentativeFilters, 0.0.1-preview9"
#:package ArgumentativeFilters@0.0.1-preview9
#addin nuget:?package=ArgumentativeFilters&version=0.0.1-preview9&prerelease
#tool nuget:?package=ArgumentativeFilters&version=0.0.1-preview9&prerelease
Argumentative Filters
⚠️ This is an early preview package, and is not intended for production use yet. It is likely broken in a number of ways.
Background
In classic ASP.Net Core APIs, IActionFilter.ActionExecutingContext provides access to IDictionary<string, object?> ActionArguments which is a name-value collection of the parameters that will be provided to the Controller action that will be run.
There isn't a direct equivalent to that in AspNetCore Minimal APIs, instead, an EndpointFilterInvocationContext is passed, which instead has in IList<object?> Arguments for accessing the Minimal API endpoint delegate argument values.
To be able to access arguments by name, a filter factory must be created that takes an EndpointFilterFactoryContext which contains a MethodInfo representing the endpoint delegate, from which you can retrieve the parameter position of the parameters by name, and use that to index into the Arguments collection.
If that sounds a bit labour intensive, that's because it is. That's where Argumentative Filters come in.
With Argumentative Filters, a filter can be created as a static method that takes an EndpointFilterInvocationContext and an EndpointFilterDelegate as required parameters. Other kinds of parameters are also supported and documented below.
Roadmap/Wishlist
- Work in non-trivial cases 😅
- Have tests
- Support nested classes
- Provide usage analyzers
- Allow specifying preferred scope for injected services (Can exclude scoped services and resolve services from
EndpointFilterFactoryContext.ApplcationServices) - Provide some way to define fallback behaviour if filter arguments cannot be matched
Usage
Filter
public static partial class ExampleFilter
{
[ArgumentativeFilter]
public static ValueTask<object?> NormalizeParameterCase(EndpointFilterInvocationContext context, EndpointFilterDelegate next, string parameter, [IndexOf(nameof(parameter))] int parameterIndex)
{
context.Arguments[parameterIndex] = parameter.ToUpperInvariant();
return next(context);
}
}
Minimal API Route Configuration
app.MapGet("/my/route/{parameter}", (string parameter) => parameter)
.AddEndpointFilterFactory(ExampleFilter.Factory);
Parameter Modifiers
IndexOf
This source generator provides an IndexOfAttribute with a single constructor argument, which is the endpoint argument to populate the annotated parameter with.
[IndexOf(nameof(parameter))] int parameterIndex
This can be useful for cases where the the filter is going to be used to mutate endpoint parameters.
FromServices
This source generator has special handling for parameters annotated with the Microsoft.AspNetCore.Mvc.FromServicesAttribute.
When a parameter has this attribute, the generated factory will attempt to resolve an instance of the parameter type from the HttpContext.RequestServices scoped service provider.
Nullability and Optionallity are respected when doing this.
Note 📓
- In nullable reference type contexts if the parameter is marked as nullable,
IServiceProvider.GetServiceis used, otherwiseIServiceProvider.GetRequiredServiceis used, and the factory will throw if the service cannot be resolved.- In contexts where nullable reference types are not enabled
IServiceProvider.GetRequiredServicewill be used unless the parameter is optional.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.13 | 488 | 9/23/2025 |
| 0.0.12 | 3,725 | 7/2/2024 |
| 0.0.11 | 384 | 3/30/2024 |
| 0.0.11-preview.1 | 82 | 3/29/2024 |
| 0.0.10 | 197 | 3/18/2024 |
| 0.0.9 | 197 | 3/7/2024 |
| 0.0.9-preview.1 | 91 | 3/6/2024 |
| 0.0.8 | 393 | 1/21/2024 |
| 0.0.7 | 329 | 1/3/2024 |
| 0.0.6 | 334 | 11/2/2023 |
| 0.0.5 | 278 | 9/13/2023 |
| 0.0.4 | 330 | 8/4/2023 |
| 0.0.4-preview.2 | 151 | 7/15/2023 |
| 0.0.4-preview.1 | 162 | 7/7/2023 |
| 0.0.3 | 258 | 6/15/2023 |
| 0.0.2-preview.2 | 144 | 5/16/2023 |
| 0.0.2-preview.1 | 149 | 5/16/2023 |
| 0.0.2-preview | 239 | 5/14/2023 |
| 0.0.1-preview9 | 287 | 4/16/2023 |
| 0.0.1-preview8 | 308 | 4/12/2023 |
| 0.0.1-preview7 | 261 | 4/11/2023 |
| 0.0.1-preview6 | 276 | 4/10/2023 |