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.
Install using the ZNetCS.AspNetCore.Compression NuGet package
PM> Install-Package ZNetCS.AspNetCore.Compression
When you install the package, it should be added to your .csproj. Alternatively, you can add it directly by adding:
<ItemGroup>
<PackageReferenceInclude="ZNetCS.AspNetCore.Compression"Version="10.0.0" />
</ItemGroup>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=newList<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=newList<string>{"/css/","/images/","/js/","/lib/"};options.MinimumCompressionThreshold=860;options.Compressors=newList<ICompressor>{newBrotliCompressor(),newGZipCompressor(),newDeflateCompressor()};options.Decompressors=newList<IDecompressor>{newBrotliDecompressor(),newGZipDecompressor(),newDeflateDecompressor()};});then
// Configure IP filteringapp.UseCompression();In order to use the Compression middleware, you must configure the services in the ConfigureServices and Configure call of Startup:
publicvoidConfigureServices(IServiceCollectionservices){services.AddCompression();}publicvoidConfigure(IApplicationBuilderapp){app.UseCompression();// other middleware e.g. MVC etc}You can alternatively setup additional options for compression and decompression
publicvoidConfigure(IApplicationBuilderapp){app.UseCompression();// other middleware e.g. MVC etc }publicvoidConfigureServices(IServiceCollectionservices){services.AddCompression(
options =>{options.AllowedMediaTypes=newList<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=newList<string>{"/css/","/images/","/js/","/lib/"};options.MinimumCompressionThreshold=860;options.Compressors=newList<ICompressor>{newBrotliCompressor(),newGZipCompressor(),newDeflateCompressor()};options.Decompressors=newList<IDecompressor>{newBrotliDecompressor(),newGZipDecompressor(),newDeflateDecompressor()};});}The default options when empty constructor is used are listed above.
Compressors also allow to specify compression level.
publicvoidConfigure(IApplicationBuilderapp){app.UseCompression();// other middleware e.g. MVC etc }publicvoidConfigureServices(IServiceCollectionservices){services.AddCompression(
options =>{options.Compressors=newList<ICompressor>{newBrotliDecompressor(CompressionLevel.Fastest),newGZipCompressor(CompressionLevel.Fastest),newDeflateCompressor(CompressionLevel.Fastest)};});}