Swashbuckle.AspNetCore.JsonMultipartFormDataSupport 1.4.2

There is a newer version of this package available.
See the version list below for details.
Install-Package Swashbuckle.AspNetCore.JsonMultipartFormDataSupport -Version 1.4.2
dotnet add package Swashbuckle.AspNetCore.JsonMultipartFormDataSupport --version 1.4.2
<PackageReference Include="Swashbuckle.AspNetCore.JsonMultipartFormDataSupport" Version="1.4.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Swashbuckle.AspNetCore.JsonMultipartFormDataSupport --version 1.4.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Swashbuckle.AspNetCore.JsonMultipartFormDataSupport, 1.4.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Swashbuckle.AspNetCore.JsonMultipartFormDataSupport as a Cake Addin
#addin nuget:?package=Swashbuckle.AspNetCore.JsonMultipartFormDataSupport&version=1.4.2

// Install Swashbuckle.AspNetCore.JsonMultipartFormDataSupport as a Cake Tool
#tool nuget:?package=Swashbuckle.AspNetCore.JsonMultipartFormDataSupport&version=1.4.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

https://www.nuget.org/packages/Swashbuckle.AspNetCore.JsonMultipartFormDataSupport

Swashbuckle.AspNetCore.JsonMultipartFormDataSupport

Adds support for json in multipart/form-data requests.

Exmaple

Usage

  1. Simple add this to your ConfigureServices
services.AddJsonMultipartFormDataSupport();

Or manually:

  • Binder
services
    .AddMvc(
        properties => {
            // ...
            properties.ModelBinderProviders.Insert(0, new FormDataJsonBinderProvider()); // Here
        }
    )
  • Operation filter
services.AddSwaggerGen(c => {
        c.OperationFilter<MultiPartJsonOperationFilter>();
    });
  1. Add to your Controller
[HttpPost]
[Consumes("multipart/form-data")] 
public async Task<IActionResult> Post([FromForm] MultipartFormData<Product> multiPartData) {
    var file = multiPartData.File;
    var product = multiPartData.Json;
}

or

[HttpPost]
[Consumes("multipart/form-data")] 
public async Task<IActionResult> Post([FromForm] MultipartRequiredFormData<Product> multiPartData) {
    var file = multiPartData.File;
    var product = multiPartData.Json;
}

Or you can create your on wrapper

public class MyWrapper {
    [FromJson] // <-- This attribute is required for binding.
    public MyModel Json { get; set; }

    public IFormFile File { get; set; }
}

and then

[HttpPost]
[Consumes("multipart/form-data")] 
public async Task<IActionResult> Post([FromForm] MyWrapper myWrapper) {
    // code
}

Notes:

It automatically adds examples from class which implements IExampleProvider<MyModel>.

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
1.5.0 1,673 6/28/2021
1.4.2 10,423 9/22/2020
1.4.1 1,146 6/20/2020
1.4.0 3,193 4/6/2020
1.3.0 1,265 1/13/2020
1.2.3 224 1/10/2020
1.2.2 230 1/9/2020
1.2.1 214 1/9/2020
1.2.0 242 1/9/2020
1.1.2 240 11/25/2019
1.1.1 214 11/6/2019
1.1.0 234 10/29/2019
1.0.1 283 9/25/2019
1.0.0 227 9/25/2019

## [1.4.2] - 2020-09-22
### Fixed
- Now invalid json returns null instead of throwing JsonParser exception and causing HTTP 500