Two additional middleware components were released with ASP.NET Core 1.1, one for response caching and the other for response compression.
- [Instructor] Two additional middleware components in 1.1 are used for response caching and response compression. Let's look at response caching first. It requires the Microsoft.AspNetCore.Response caching package, and its usage is very simple. Add services.AddResponseCaching in ConfigureServices, and there are some options available, add app.UseResponseCaching in the configure method, and those are both of course in the startup class, and then use the ResponseCache controller attribute.
The three options available to set are UseCaseSensitivePaths, and that defaults to false, the MaximumBodySize, and it defauts to 64 megabytes, and the SizeLimit for the cache, and that defaults to 100 megabytes. There is a slight modification in 2.0 and that is where you can configure the response cache in middleware. So let's look at this in code. So I am in the startup class in the ConfigureServices method, and on like 66 I'm adding response caching with the default options set.
I have an example here of how you would customize it if you so choose. Then we go down to the Configure method, and right here on line 115 I add app.UseReponseCaching, and then on the HomeController I have ResponseCache, duration equals 30. I'm going to use Fiddler to test this just cause it's easier and clearer to see. So I'm going to go to the homepage right here, and I hit execute, and we look at the response, and in the header right here we see the cache control, the max age of 30.
So very simply we added caching into our site. Response compression is also very easy to add in to .NET Core. It requires the Microsoft.AspNetCore.Response compression package, and then, just like we did with the caching, you add services.AddResponseCompression in ConfigureServices, app.UseResponseCompression in the Configure method, and then you configure a compression provider.
Just as a side note with the sample code, if you're using a modern browser and the user agent contains that string listed on your screen, the AppleWebKit, blah, blah, blah, then it will not actually compress the response. So we're going to use Fiddler to test this as well so that we can see very clearly that it is getting compressed. Just like the response caching middleware, in 2.0 the main change is where it gets configured.
So let's now look at this in code. So once again I'm in the startup class in the ConfigureServices method, and here on line 74 I just add services.AddResponseCompression, and by default that will use the gzip compression provider, and you can also configure it. So I have sample code here on line 75 through 82 where you can change things like the compression level and the accepted MIME types.
Moving on to the Configure method, and we have app.UseResponseCompression. One important note is, as we discussed when we were talking about middleware, order is very important. So you always want to put the caching in the compression at the very top of these methods. So let's look at how this effects our site. I'm already running. I'll bring up Fiddler, go in the composer. In the accept encoding you have to have gzip in there, and so let's execute this.
And let's look at the response. We can see that it is all very hard to read. Go into the header and the content encoding is indeed gzip. So here with one additional package and two lines of code we have now brought compression into our application.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages