ArgumentativeFilters 0.0.1-preview9

This is a prerelease version of ArgumentativeFilters.
There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ArgumentativeFilters" Version="0.0.1-preview9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ArgumentativeFilters" Version="0.0.1-preview9" />
                    
Directory.Packages.props
<PackageReference Include="ArgumentativeFilters" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ArgumentativeFilters --version 0.0.1-preview9
                    
#r "nuget: ArgumentativeFilters, 0.0.1-preview9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ArgumentativeFilters@0.0.1-preview9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ArgumentativeFilters&version=0.0.1-preview9&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ArgumentativeFilters&version=0.0.1-preview9&prerelease
                    
Install as a Cake Tool

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.GetService is used, otherwise IServiceProvider.GetRequiredService is used, and the factory will throw if the service cannot be resolved.
  • In contexts where nullable reference types are not enabled IServiceProvider.GetRequiredService will be used unless the parameter is optional.
There are no supported framework assets in this package.

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