Permalink
Browse files

Changes needed to use Brotli in ASP.NET Core

  • Loading branch information...
christopher.demicoli
christopher.demicoli committed Jul 29, 2017
1 parent 61d6d26 commit 5605681e76f11214cc0f6eedecbf4e5ce3e97de7
@@ -1,9 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
VisualStudioVersion = 15.0.26430.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brotli-ASP.NET-Core-Compression", "Brotli-ASP.NET-Core-Compression\Brotli-ASP.NET-Core-Compression.csproj", "{FABF9A3D-A219-486C-B912-0B479ECF2208}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brotli-ASP.NET-Core-Compression", "Brotli-ASP.NET-Core-Compression\Brotli-ASP.NET-Core-Compression.csproj", "{FABF9A3D-A219-486C-B912-0B479ECF2208}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F053F5A9-7BA6-4C62-A76A-2B4515EC4DBD}"
ProjectSection(SolutionItems) = preProject
NuGet.Config = NuGet.Config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
@@ -11,9 +11,11 @@
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="System.IO.Compression.Brotli" Version="0.1.0-e170729-1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.ResponseCompression;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;

namespace Brotli_ASP.NET_Core_Compression
{
public class BrotliCompressionProvider : ICompressionProvider
{
public string EncodingName => "br";
public bool SupportsFlush => true;
public Stream CreateStream(Stream outputStream)
{
return new BrotliStream(outputStream, CompressionMode.Compress);
}
}
}
@@ -7,6 +7,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.ResponseCompression;

namespace Brotli_ASP.NET_Core_Compression
{
@@ -29,6 +30,12 @@ public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();

services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" });
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -47,6 +54,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
app.UseExceptionHandler("/Home/Error");
}

app.UseResponseCompression();
app.UseStaticFiles();

app.UseMvc(routes =>
@@ -0,0 +1,5 @@
<configuration>
<packageSources>
<add key="dotnet.myget.org dotnet-corefxlab" value="https://dotnet.myget.org/F/dotnet-corefxlab/" />
</packageSources>
</configuration>

0 comments on commit 5605681

Please sign in to comment.