Ⅰ. はじめに
タイトルの通り「ASP.NET Coreでレスポンスを圧縮する方法」です。
Ⅱ. 手順
1. サンプルプログラムを書く
Program.cs
using Microsoft.AspNetCore.ResponseCompression; using System.IO.Compression; var builder = WebApplication.CreateBuilder(args); builder.Services.AddResponseCompression(); builder.Services.AddResponseCompression(options => { options.EnableForHttps = true; options.Providers.Add<BrotliCompressionProvider>(); options.Providers.Add<GzipCompressionProvider>(); }); builder.Services.Configure<BrotliCompressionProviderOptions>(options => { options.Level = CompressionLevel.Fastest; }); var app = builder.Build(); app.UseResponseCompression(); app.MapGet("/api/test", () => { return "Hello world"; }); app.Run();
実行結果
Brotli | |
gzip |