diff --git a/aspnetcore/fundamentals/configuration/index.md b/aspnetcore/fundamentals/configuration/index.md index 79626451ff..7271d764a7 100644 --- a/aspnetcore/fundamentals/configuration/index.md +++ b/aspnetcore/fundamentals/configuration/index.md @@ -212,7 +212,7 @@ The [CommandLine configuration provider](/aspnet/core/api/microsoft.extensions.c # [Basic Configuration](#tab/basicconfiguration) -To activate command-line configuration, call the `AddCommandLine` extension method on an instance of [ConfigurationBuilder](/api/microsoft.extensions.configuration.configurationbuilder): +To activate command-line configuration, call the `AddCommandLine` extension method on an instance of [ConfigurationBuilder](/dotnet/api/microsoft.extensions.configuration.configurationbuilder): [!code-csharp[Main](index/sample_snapshot//CommandLine/Program.cs?highlight=18,21)] diff --git a/aspnetcore/performance/caching/middleware.md b/aspnetcore/performance/caching/middleware.md index 8139c4d0a5..df2a312f9b 100644 --- a/aspnetcore/performance/caching/middleware.md +++ b/aspnetcore/performance/caching/middleware.md @@ -1,10 +1,11 @@ --- title: Response Caching Middleware in ASP.NET Core author: guardrex -description: Learn how to configure and use Response Caching Middleware in ASP.NET Core apps. +description: Learn how to configure and use Response Caching Middleware in ASP.NET Core. ms.author: riande manager: wpickett -ms.date: 08/22/2017 +ms.custom: mvc +ms.date: 01/26/2017 ms.topic: article ms.prod: asp.net-core uid: performance/caching/middleware @@ -13,44 +14,31 @@ uid: performance/caching/middleware By [Luke Latham](https://github.com/guardrex) and [John Luo](https://github.com/JunTaoLuo) -[View or download sample code](https://github.com/aspnet/Docs/tree/master/aspnetcore/performance/caching/middleware/samples) ([how to download](xref:tutorials/index#how-to-download-a-sample)) +[View or download sample code](https://github.com/aspnet/Docs/tree/master/aspnetcore/performance/caching/middleware/sample) ([how to download](xref:tutorials/index#how-to-download-a-sample)) -This document provides details on how to configure the Response Caching Middleware in ASP.NET Core apps. The middleware determines when responses are cacheable, stores responses, and serves responses from cache. For an introduction to HTTP caching and the `ResponseCache` attribute, see [Response Caching](response.md). +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` attribute, see [Response Caching](xref:performance/caching/response). ## Package -To include the middleware in a project, add a reference to the [`Microsoft.AspNetCore.ResponseCaching`](https://www.nuget.org/packages/Microsoft.AspNetCore.ResponseCaching/) package or use the [`Microsoft.AspNetCore.All`](https://www.nuget.org/packages/Microsoft.AspNetCore.All/) package. + +To include the middleware in a project, add a reference to the [`Microsoft.AspNetCore.ResponseCaching`](https://www.nuget.org/packages/Microsoft.AspNetCore.ResponseCaching/) package or use the [`Microsoft.AspNetCore.All`](https://www.nuget.org/packages/Microsoft.AspNetCore.All/) package (ASP.NET Core 2.0 or later when targeting .NET Core). ## Configuration + In `ConfigureServices`, add the middleware to the service collection. -# [ASP.NET Core 2.x](#tab/aspnetcore2x) - -[!code-csharp[Main](middleware/samples/2.x/Program.cs?name=snippet1&highlight=4)] - -# [ASP.NET Core 1.x](#tab/aspnetcore1x) - -[!code-csharp[Main](middleware/samples/1.x/Startup.cs?name=snippet1&highlight=3)] - ---- +[!code-csharp[Main](middleware/sample/Startup.cs?name=snippet1&highlight=3)] Configure the app to use the middleware with the `UseResponseCaching` extension method, which adds the middleware to the request processing pipeline. The sample app adds a [`Cache-Control`](https://tools.ietf.org/html/rfc7234#section-5.2) header to the response that caches cacheable responses for up to 10 seconds. The sample sends a [`Vary`](https://tools.ietf.org/html/rfc7231#section-7.1.4) header to configure 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. -# [ASP.NET Core 2.x](#tab/aspnetcore2x) +[!code-csharp[Main](middleware/sample/Startup.cs?name=snippet2&highlight=3,7-12)] -[!code-csharp[Main](middleware/samples/2.x/Program.cs?name=snippet1&highlight=8)] - -# [ASP.NET Core 1.x](#tab/aspnetcore1x) - -[!code-csharp[Main](middleware/samples/1.x/Startup.cs?name=snippet2&highlight=3)] - ---- - -The Response Caching Middleware only caches 200 (OK) server responses. Any other responses, including [error pages](xref:fundamentals/error-handling), are ignored by the middleware. +Response Caching Middleware only caches server responses that result in a 200 (OK) status code. Any other responses, including [error pages](xref:fundamentals/error-handling), are ignored by the middleware. > [!WARNING] > Responses containing content for authenticated clients must be marked as not cacheable to prevent the middleware from storing and serving those responses. See [Conditions for caching](#conditions-for-caching) for details on how the middleware determines if a response is cacheable. ## Options + The middleware offers three options for controlling response caching. | Option | Default Value | @@ -59,7 +47,10 @@ The middleware offers three options for controlling response caching. | MaximumBodySize | The largest cacheable size for the response body in bytes.
The default value is `64 * 1024 * 1024` (64 MB). | | SizeLimit | The size limit for the response cache middleware in bytes. The default value is `100 * 1024 * 1024` (100 MB). | -The following example configures the middleware to cache responses smaller than or equal to 1,024 bytes using case-sensitive paths, storing the responses to `/page1` and `/Page1` separately. +The following example configures the middleware to: + +* Cache responses smaller than or equal to 1,024 bytes. +* Store the responses by case-sensitive paths (for example, `/page1` and `/Page1` are stored separately). ```csharp services.AddResponseCaching(options => @@ -70,9 +61,10 @@ services.AddResponseCaching(options => ``` ## VaryByQueryKeys -When using MVC, the `ResponseCache` attribute specifies the parameters necessary for setting appropriate headers for response caching. The only parameter of the `ResponseCache` attribute that strictly requires the middleware is `VaryByQueryKeys`, which doesn't correspond to an actual HTTP header. For more information, see [ResponseCache Attribute](response.md#responsecache-attribute). -When not using MVC, you can vary response caching with the `VaryByQueryKeys` feature. Use the `ResponseCachingFeature` directly from the `IFeatureCollection` of the `HttpContext`: +When using MVC/Web API controllers or Razor Pages page models, the `ResponseCache` attribute specifies the parameters necessary for setting the appropriate headers for response caching. The only parameter of the `ResponseCache` attribute that strictly requires the middleware is `VaryByQueryKeys`, which doesn't correspond to an actual HTTP header. For more information, see [ResponseCache Attribute](xref:performance/caching/response#responsecache-attribute). + +When not using the `ResponseCache` attribute, response caching can be varied with the `VaryByQueryKeys` feature. Use the `ResponseCachingFeature` directly from the `IFeatureCollection` of the `HttpContext`: ```csharp var responseCachingFeature = context.HttpContext.Features.GetCurrent server time at time of page generation: @System.DateTime.Now
+The app responds with its Index page, including a Cache-Control
header to configure caching behavior. The app also sets the Vary
header to configure the cache to serve the response only if the Accept-Encoding
header of subsequent requests matches that from the original request.
When running the sample, the Index page is served from cache when stored and cached for up to 10 seconds.
+Don't use a browser to test caching behavior. Browsers often add a Cache-Control
header (for example, max-age=0
) on reload that prevent the middleware from serving a cached page. To test caching behavior, use a tool like Fiddler, Firebug, or Postman.