Enabling Compression (Brotli/Gzip) in ASP.NET Core Print

  • 0

Response Compression

Enable compression to reduce bandwidth and improve load times for your ASP.NET Core applications on Plesk.

Why Use Compression?

  • Reduces response size by 60-90%
  • Faster page loads for users
  • Lower bandwidth costs
  • Better SEO rankings

Adding Response Compression

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true;  // Enable for HTTPS
    options.Providers.Add();
    options.Providers.Add();
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
        new[] { "application/json", "text/plain", "text/css", "application/javascript" });
});

builder.Services.Configure(options =>
{
    options.Level = CompressionLevel.Fastest;  // Or Optimal for better compression
});

builder.Services.Configure(options =>
{
    options.Level = CompressionLevel.Fastest;
});

var app = builder.Build();

// Add compression early in the pipeline
app.UseResponseCompression();
app.UseStaticFiles();
// ... rest of middleware

Compression Providers

ProviderCompressionBrowser SupportSpeed
BrotliBestModern browsersSlower
GzipGoodAll browsersFast

IIS Static Compression

IIS can also compress static files. Add to web.config:

<system.webServer>
  <urlCompression doStaticCompression="true" doDynamicCompression="true" />
  <httpCompression>
    <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="application/json" enabled="true" />
    </staticTypes>
  </httpCompression>
</system.webServer>

Blazor WebAssembly Compression

Blazor WASM files benefit greatly from compression. Ensure your web.config has:

<staticContent>
  <remove fileExtension=".br" />
  <mimeMap fileExtension=".br" mimeType="application/brotli" />
  <remove fileExtension=".gz" />
  <mimeMap fileExtension=".gz" mimeType="application/gzip" />
</staticContent>

When NOT to Compress

  • Already compressed formats (images, videos, ZIP files)
  • Very small responses (<1KB)
  • Real-time streaming content

Testing Compression

Check if compression is working:

  1. Open browser Developer Tools (F12)
  2. Go to Network tab
  3. Load your page
  4. Click a request and check Response Headers for Content-Encoding: br or gzip

Best Practices

  • Use Brotli for modern apps (better compression)
  • Include Gzip as fallback for older browsers
  • Use CompressionLevel.Fastest for dynamic content
  • Use CompressionLevel.Optimal for static content
  • Enable for HTTPS (most traffic)

Was this answer helpful?

« Back