ASP.NET MVC formatters
This library provides MessagePack-based formatters for ASP.NET MVC, offering significant performance improvements over the default JSON protocol.
Benefits
- Smaller Payloads: MessagePack produces significantly smaller payloads compared to JSON
- Faster Serialization: Binary serialization is typically faster than text-based formats
- Type Safety: Leverages MessagePack's type-safe serialization system
- NativeAOT Compatible: Works seamlessly with .NET Native AOT compilation
Installation
Install the NuGet package:
<PackageReference Include="Nerdbank.MessagePack.AspNetCoreMvcFormatter" Version="x.x.x" />
Add MessagePackInputFormatter and/or MessagePackOutputFormatter to your input and/or output formatter collections respectively, as demonstrated in the configuration sample below:
using Nerdbank.MessagePack.AspNetCoreMvcFormatter;
using PolyType;
void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions(option =>
{
option.OutputFormatters.Clear();
option.OutputFormatters.Add(new MessagePackOutputFormatter(Witness.ShapeProvider));
option.InputFormatters.Clear();
option.InputFormatters.Add(new MessagePackInputFormatter(Witness.ShapeProvider));
});
}
[GenerateShapeFor<bool>] // add an attribute for each top-level type that must be serializable
partial class Witness;
Usage
The JavaScript client should send data with application/x-msgpack
as the HTTP Content-Type
header.
The Accept
header should include this same content-type so that the server will utilize the MessagePack formatter to send the optimized data format back to the client.