Created
March 10, 2018 07:43
-
-
Save wellingtonjhn/a15306d95a161328cbec404e356a446c to your computer and use it in GitHub Desktop.
Custom Response Compression ASP.Net Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomCompressionProvider : ICompressionProvider | |
| { | |
| public string EncodingName => "mycustomcompression"; | |
| public bool SupportsFlush => true; | |
| public Stream CreateStream(Stream outputStream) | |
| { | |
| return outputStream; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Startup | |
| { | |
| public Startup(IConfiguration configuration) | |
| { | |
| Configuration = configuration; | |
| } | |
| public IConfiguration Configuration { get; } | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddResponseCompression(options => | |
| { | |
| options.Providers.Add<GzipCompressionProvider>(); | |
| options.Providers.Add<CustomCompressionProvider>(); | |
| options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" }); | |
| }); | |
| services.Configure<GzipCompressionProviderOptions>(options => | |
| { | |
| options.Level = CompressionLevel.Fastest; | |
| }); | |
| services.AddMvc(); | |
| } | |
| public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
| { | |
| if (env.IsDevelopment()) | |
| { | |
| app.UseDeveloperExceptionPage(); | |
| } | |
| app.UseResponseCompression(); | |
| app.UseMvc(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment