ZNetCS.AspNetCore.Compression 6.0.0

.NET 5.0 .NET Core 3.1
Install-Package ZNetCS.AspNetCore.Compression -Version 6.0.0
dotnet add package ZNetCS.AspNetCore.Compression --version 6.0.0
<PackageReference Include="ZNetCS.AspNetCore.Compression" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ZNetCS.AspNetCore.Compression --version 6.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZNetCS.AspNetCore.Compression, 6.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 ZNetCS.AspNetCore.Compression as a Cake Addin
#addin nuget:?package=ZNetCS.AspNetCore.Compression&version=6.0.0

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

ZNetCS.AspNetCore.Compression

NuGet Build

A small package to allow decompress incoming request and compress outgoing response inside ASP.NET Core application. This package by default supports Brotli, GZIP and Deflate compression and decompression.

This package compresses all content in memory before sending it to client to provide new Content-Length.

Installing

Install using the ZNetCS.AspNetCore.Compression NuGet package

PM> Install-Package ZNetCS.AspNetCore.Compression

Usage

When you install the package, it should be added to your .csproj. Alternatively, you can add it directly by adding:

<ItemGroup>
    <PackageReference Include="ZNetCS.AspNetCore.Compression" Version="6.0.0" />
</ItemGroup>

.NET 6

In order to use the Compression middleware, you must configure the services in the Program.cs file.

// Add services to the container.
builder.Services.AddCompression();

or

// Add services to the container.
builder.Services.AddCompression(
        options =>
        {
            options.AllowedMediaTypes = new List<MediaTypeHeaderValue>
            {
                MediaTypeHeaderValue.Parse("text/*"),
                MediaTypeHeaderValue.Parse("message/*"),
                MediaTypeHeaderValue.Parse("application/x-javascript"),
                MediaTypeHeaderValue.Parse("application/javascript"),
                MediaTypeHeaderValue.Parse("application/json"),
                MediaTypeHeaderValue.Parse("application/xml"),
                MediaTypeHeaderValue.Parse("application/atom+xml"),
                MediaTypeHeaderValue.Parse("application/xaml+xml")
            };
            options.IgnoredPaths = new List<string>
            {
                "/css/",
                "/images/",
                "/js/",
                "/lib/"
            };
            options.MinimumCompressionThreshold = 860;
            options.Compressors = new List<ICompressor> { new BrotliCompressor(), new GZipCompressor(), new DeflateCompressor() };
            options.Decompressors = new List<IDecompressor> { new BrotliDecompressor(), new GZipDecompressor(), new DeflateDecompressor() };
        });

then

// Configure IP filtering
app.UseCompression();

.NET 5 and Below

In order to use the Compression middleware, you must configure the services in the ConfigureServices and Configure call of Startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCompression();
}

public void Configure(IApplicationBuilder app)
{
    app.UseCompression();

    // other middleware e.g. MVC etc
}

You can alternatively setup additional options for compression and decompression

public void Configure(IApplicationBuilder app)
{
    app.UseCompression();

    // other middleware e.g. MVC etc  
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddCompression(
        options =>
        {
            options.AllowedMediaTypes = new List<MediaTypeHeaderValue>
            {
                MediaTypeHeaderValue.Parse("text/*"),
                MediaTypeHeaderValue.Parse("message/*"),
                MediaTypeHeaderValue.Parse("application/x-javascript"),
                MediaTypeHeaderValue.Parse("application/javascript"),
                MediaTypeHeaderValue.Parse("application/json"),
                MediaTypeHeaderValue.Parse("application/xml"),
                MediaTypeHeaderValue.Parse("application/atom+xml"),
                MediaTypeHeaderValue.Parse("application/xaml+xml")
            };
            options.IgnoredPaths = new List<string>
            {
                "/css/",
                "/images/",
                "/js/",
                "/lib/"
            };
            options.MinimumCompressionThreshold = 860;
            options.Compressors = new List<ICompressor> { new BrotliCompressor(), new GZipCompressor(), new DeflateCompressor() };
            options.Decompressors = new List<IDecompressor> { new BrotliDecompressor(), new GZipDecompressor(), new DeflateDecompressor() };
        });
}

The default options when empty constructor is used are listed above.

Compressors also allow to specify compression level.

public void Configure(IApplicationBuilder app)
{
    app.UseCompression();

    // other middleware e.g. MVC etc  
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddCompression(
        options =>
        {
            options.Compressors = new List<ICompressor> { new BrotliDecompressor(CompressionLevel.Fastest), new GZipCompressor(CompressionLevel.Fastest), new DeflateCompressor(CompressionLevel.Fastest) };
        });
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

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
6.0.0 1,134 2/3/2022
2.2.0 52,484 10/11/2019
2.0.2 18,197 1/9/2018
2.0.1 694 1/2/2018
2.0.0 862 8/29/2017
1.0.4 2,598 2/25/2017
1.0.3 935 12/23/2016
1.0.2 844 10/27/2016
1.0.1 855 10/14/2016
1.0.0 679 9/29/2016

Breaking Change: Drop support for netstandard and .net framework. Code refactoring. Dependency update. Nullable enable. Logging performance improvements.