ApiMultipartFormDataFormatter 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApiMultipartFormDataFormatter --version 2.0.0
NuGet\Install-Package ApiMultipartFormDataFormatter -Version 2.0.0
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="ApiMultipartFormDataFormatter" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiMultipartFormDataFormatter --version 2.0.0
#r "nuget: ApiMultipartFormDataFormatter, 2.0.0"
#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.
// Install ApiMultipartFormDataFormatter as a Cake Addin
#addin nuget:?package=ApiMultipartFormDataFormatter&version=2.0.0

// Install ApiMultipartFormDataFormatter as a Cake Tool
#tool nuget:?package=ApiMultipartFormDataFormatter&version=2.0.0

ApiMultipartFormDataFormatter Build status

Features:

  • Receives multipart/form-data request from client, parses information and bind to model.

Implementation:


WEB API 2 implementation:

To use this custom media format in your WEB API 2 project. Please following these steps:

  • Install the lastest nuget package by using command Install-Package ApiMultipartFormDataFormatter.

  • Open WebApiConfig.cs file and add the following command: config.Formatters.Add(new MultipartFormDataFormatter());

    WebApiConfig.cs:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
    
        // Web API routes
        config.MapHttpAttributeRoutes();
    
        config.Routes.MapHttpRoute(
            "DefaultApi",
            "api/{controller}/{id}",
            new {id = RouteParameter.Optional}
        );
    
        config.Formatters.Add(new MultipartFormDataFormatter());
    }
    

    In controller file

    [RoutePrefix("api/account")]
    public class ApiAccountController : ApiController
    {
        [Route("register")]
        [HttpPost]
        public HttpResponseMessage Register(AccountRegistrationViewModel parameters)
        {
            if (parameters == null)
            {
                parameters = new AccountRegistrationViewModel();
                Validate(parameters);
            }
    
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
    
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
    
  • Start posting a multipart/form-data to your WEB API 2 project and enjoy.

Note:

  • To specify a parameter is a file, please use HttpFile class.
    • For example:
public class Category
{
	public int Id { get; set; }

	public List<Photo> Photos { get; set; }
}

public class AccountRegistrationViewModel
{
  /// <summary>
  /// Account owner.
  /// </summary>
  [Required]
  public Owner Owner { get; set; }

  /// <summary>
  /// User age.
  /// </summary>
  [Required]
  public int Age { get; set; }

  /// <summary>
  /// Account photo.
  /// </summary>
  [Required]
  public HttpFile Photo { get; set; }
}
    

Implementation of IMultiPartFormDataModelBinderService

  • In version 2.0.0, this plugin uses IMultiPartFormDataModelBinderService to help developer override multpart-form/data parameter serialization.
  • You can take a look at basic implemenation of class BaseMultiPartFormDataModelBinderService to know how to implement it:
public class BaseMultiPartFormDataModelBinderService : IMultiPartFormDataModelBinderService
{
    #region Methods

    /// <summary>
    /// <inheritdoc />
    /// </summary>
    /// <param name="propertyInfo"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public object BuildModel(PropertyInfo propertyInfo, object value)
    {
        // Property is not defined.
        if (propertyInfo == null)
            return null;

        // Get property type.
        var propertyType = propertyInfo.PropertyType;

        // Property is GUID.
        if (propertyType == typeof(Guid) && Guid.TryParse(value.ToString(), out var guid))
            return guid;

        return Convert.ChangeType(value, propertyType);
    }

    #endregion
}

Limitation:

  • Currently, this formatter cannot deal with interfaces such as: IAccount, IList, IEnumerable, ... To use it with a collection, please specify : List, Enumerable, ....
  • For a collection, please use List, Enumerable, ... instead of [] (array). This feature will be updated later.

Change log:

  • 1.0.4:

    • Fixed bug #3 : Bad request while trying casting string to GUID
    • Added IMultiPartFormDataModelBinderService for intercepting model serialization.
    • ApiMultipartFormDataFormatter now is obsoleted. An exception is thrown if this class is used. Please using MultipartFormDataFormatter instead.
    • FindContentDispositionParametersInterceptor: Allow developer to custmize how parameter will be serialized. Please take a look at MultipartFormDataFormatter.cs.
  • 1.0.3:

    • Prevent dependencies such as NewtonSoft.Json, ... from being compiled and included in release nuget package. Therefore, the package size is smaller.
    • Prevent dependencies from being removed when ApiMultipartFormDataFormatter nuget is uninstalled.
  • 1.0.1:

    • Fixed issue about list serialization, mentioned here
  • 1.0.2:

    • Incorrect release version. Please skip this.
  • 1.0.0:

    • Initial release.

IMPORTANT NOTE:

  • While sending the request, please make sure not to attach Content-Type in header or make Content-Type be NULL
  • ApiMultipartFormDataFormatter is obsolete and will be removed in version after 1.0.3. Please use MultipartFormDataFormatter instead.

Images:

Postman request

Parameter analyzation

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
3.0.0 316,101 3/19/2020
3.0.0-Preview-001 637 1/9/2020
2.1.0 42,867 9/22/2019
2.0.0 12,036 7/6/2019
1.0.3 18,761 6/18/2018
1.0.2 980 6/13/2018
1.0.0 11,582 3/22/2017

New features and bug fixing.