ApiMultipartFormDataFormatter 2.0.0

There is a newer version of this package available.
See the version list below for details.
Install-Package ApiMultipartFormDataFormatter -Version 2.0.0
dotnet add package ApiMultipartFormDataFormatter --version 2.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ApiMultipartFormDataFormatter, 2.0.0"
#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 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

There are no supported framework assets in this 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 50,976 3/19/2020
3.0.0-Preview-001 477 1/9/2020
2.1.0 18,367 9/22/2019
2.0.0 5,928 7/6/2019
1.0.3 10,815 6/18/2018
1.0.2 628 6/13/2018
1.0.0 9,527 3/22/2017

New features and bug fixing.