|
2 | 2 | title: Response Caching Middleware in ASP.NET Core
|
3 | 3 | author: rick-anderson
|
4 | 4 | description: Learn how to configure and use Response Caching Middleware in ASP.NET Core.
|
5 |
| -monikerRange: '>= aspnetcore-2.1' |
| 5 | +monikerRange: '>= aspnetcore-3.1' |
6 | 6 | ms.author: riande
|
7 | 7 | ms.custom: mvc
|
8 |
| -ms.date: 02/07/2020 |
| 8 | +ms.date: 12/07/2021 |
9 | 9 | no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
|
10 | 10 | uid: performance/caching/middleware
|
11 | 11 | ---
|
12 | 12 | # Response Caching Middleware in ASP.NET Core
|
13 | 13 |
|
14 | 14 | By [John Luo](https://github.com/JunTaoLuo)
|
15 | 15 |
|
16 |
| -::: moniker range=">= aspnetcore-3.0" |
| 16 | +::: moniker range=">= aspnetcore-6.0" |
17 | 17 |
|
18 | 18 | This article explains how to configure Response Caching Middleware in an ASP.NET Core app. The middleware determines when responses are cacheable, stores responses, and serves responses from cache. For an introduction to HTTP caching and the [`[ResponseCache]`](xref:Microsoft.AspNetCore.Mvc.ResponseCacheAttribute) attribute, see [Response Caching](xref:performance/caching/response).
|
19 | 19 |
|
@@ -160,30 +160,33 @@ When testing and troubleshooting caching behavior, a browser may set request hea
|
160 | 160 |
|
161 | 161 | ::: moniker-end
|
162 | 162 |
|
163 |
| -::: moniker range="< aspnetcore-3.0" |
| 163 | +::: moniker range="< aspnetcore-6.0" |
164 | 164 |
|
165 | 165 | This article explains how to configure Response Caching Middleware in an ASP.NET Core app. The middleware determines when responses are cacheable, stores responses, and serves responses from cache. For an introduction to HTTP caching and the [`[ResponseCache]`](xref:Microsoft.AspNetCore.Mvc.ResponseCacheAttribute) attribute, see [Response Caching](xref:performance/caching/response).
|
166 | 166 |
|
167 | 167 | [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/performance/caching/middleware/samples) ([how to download](xref:index#how-to-download-a-sample))
|
168 | 168 |
|
169 | 169 | ## Configuration
|
170 | 170 |
|
171 |
| -Use the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app) or add a package reference to the [Microsoft.AspNetCore.ResponseCaching](https://www.nuget.org/packages/Microsoft.AspNetCore.ResponseCaching/) package. |
| 171 | +Response Caching Middleware is implicitly available for ASP.NET Core apps via the shared framework. |
172 | 172 |
|
173 | 173 | In `Startup.ConfigureServices`, add the Response Caching Middleware to the service collection:
|
174 | 174 |
|
175 |
| -[!code-csharp[](middleware/samples/2.x/ResponseCachingMiddleware/Startup.cs?name=snippet1&highlight=3)] |
| 175 | +[!code-csharp[](middleware/samples/3.x/ResponseCachingMiddleware/Startup.cs?name=snippet1&highlight=3)] |
176 | 176 |
|
177 | 177 | Configure the app to use the middleware with the <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching*> extension method, which adds the middleware to the request processing pipeline in `Startup.Configure`:
|
178 | 178 |
|
179 |
| -[!code-csharp[](middleware/samples/2.x/ResponseCachingMiddleware/Startup.cs?name=snippet2&highlight=14)] |
| 179 | +[!code-csharp[](middleware/samples/3.x/ResponseCachingMiddleware/Startup.cs?name=snippet2&highlight=17)] |
| 180 | + |
| 181 | +> [!WARNING] |
| 182 | +> <xref:Microsoft.AspNetCore.Builder.CorsMiddlewareExtensions.UseCors%2A> must be called before <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching%2A> when using [CORS middleware](xref:security/cors). |
180 | 183 |
|
181 | 184 | The sample app adds headers to control caching on subsequent requests:
|
182 | 185 |
|
183 | 186 | * [Cache-Control](https://tools.ietf.org/html/rfc7234#section-5.2): Caches cacheable responses for up to 10 seconds.
|
184 | 187 | * [Vary](https://tools.ietf.org/html/rfc7231#section-7.1.4): Configures the middleware to serve a cached response only if the [Accept-Encoding](https://tools.ietf.org/html/rfc7231#section-5.3.4) header of subsequent requests matches that of the original request.
|
185 | 188 |
|
186 |
| -[!code-csharp[](middleware/samples_snippets/2.x/AddHeaders.cs)] |
| 189 | +[!code-csharp[](middleware/samples_snippets/3.x/AddHeaders.cs)] |
187 | 190 |
|
188 | 191 | The preceding headers are not written to the response and are overridden when a controller, action, or Razor Page:
|
189 | 192 |
|
@@ -302,4 +305,4 @@ When testing and troubleshooting caching behavior, a browser may set request hea
|
302 | 305 | * <xref:mvc/views/tag-helpers/builtin-th/cache-tag-helper>
|
303 | 306 | * <xref:mvc/views/tag-helpers/builtin-th/distributed-cache-tag-helper>
|
304 | 307 |
|
305 |
| -::: moniker-end |
| 308 | +::: moniker-end |
0 commit comments