Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

A library that provides Binding helpers for Minimal API projects.

License

Notifications You must be signed in to change notification settings

marcominerva/MinimalHelpers.Binding

Repository files navigation

Minimal APIs Binding Helpers

This repository is not maintained anymore. Support for IForm and IFormFile binding in Minimal APIs has been added in NET 7.0.

GitHub Super-Linter Nuget Nuget

A library that provides Binding helpers for Minimal API projects.

Installation

The library is available on NuGet. Just search MinimalHelpers.Binding in the Package Manager GUI or run the following command in the Package Manager Console:

Install-Package MinimalHelpers.Binding

Usage

IFormFile and IFormFileCollection binding

Use a FormFileContent or FormFileContentCollection argument in the route handler that must receive a single file or a collection of files, then call the corresponding Accepts* extension method on the endpoint definition:

app.MapPost("/api/single-file", (FormFileContent file) =>
{
    return Results.Ok(new
    {
        file.Content.FileName,
        file.Content.ContentType,
        file.Content.Length
    });
})
.AcceptsFormFile();

app.MapPost("/api/multiple-files", (FormFileContentCollection files) =>
{
    return Results.Ok(files.Content.Select(file => new
    {
        file.FileName,
        file.ContentType,
        file.Length
    }));
})
.AcceptsFormFileCollection();

Add the FormFile Operation Filter to Swagger, so that it will be able to correctly handle file input:

builder.Services.AddSwaggerGen(options =>
{
    options.AddFormFile();
});

Contribute

The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.