/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Middleware/ResponseCompression/sample/Startup.cs
88 строк
3 KB
Emmanuel André
Support zstd Content-Encoding (#65479)
24 фев 2026, 07:03
Не верифицирован
24 фев 2026, 07:03
2cf27e2
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.IO.Compression; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.ResponseCompression; namespace ResponseCompressionSample; public class Startup { public void ConfigureServices(IServiceCollection services) { services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest); services.Configure<ZstandardCompressionProviderOptions>(options => { options.CompressionOptions = new ZstandardCompressionOptions { Quality = 1 }; }); services.AddResponseCompression(options => { options.Providers.Add<ZstandardCompressionProvider>(); options.Providers.Add<GzipCompressionProvider>(); options.Providers.Add<CustomCompressionProvider>(); // .Append(TItem) is only available on Core. options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" }); ////Example of using excluded and wildcard MIME types: ////Compress all MIME types except various media types, but do compress SVG images. //options.MimeTypes = new[] { "*/*", "image/svg+xml" }; //options.ExcludedMimeTypes = new[] { "image/*", "audio/*", "video/*" }; }); } public void Configure(IApplicationBuilder app) { app.UseResponseCompression(); app.Map("/testfile1kb.txt", fileApp => { fileApp.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.SendFileAsync("testfile1kb.txt"); }); }); app.Map("/trickle", trickleApp => { trickleApp.Run(async context => { context.Response.ContentType = "text/plain"; // Disables compression on net451 because that GZipStream does not implement Flush. context.Features.Get<IHttpResponseBodyFeature>().DisableBuffering(); for (int i = 0; i < 100; i++) { await context.Response.WriteAsync("a"); await context.Response.Body.FlushAsync(); await Task.Delay(TimeSpan.FromSeconds(1)); } }); }); app.Run(async context => { context.Response.ContentType = "text/plain"; await context.Response.WriteAsync(LoremIpsum.Text); }); } public static Task Main(string[] args) { var host = new HostBuilder() .ConfigureWebHost(webHostBuilder => { webHostBuilder .UseKestrel() .ConfigureLogging(factory => { factory.AddConsole() .SetMinimumLevel(LogLevel.Debug); }) .UseStartup<Startup>(); }).Build(); return host.RunAsync(); } }