From 7f4c81cf674cb702f31b59a6ca8984fa2c5ffae9 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Fri, 26 Jan 2018 09:36:44 -0600 Subject: [PATCH 1/2] Fix 404 link missing the "dotnet" segment (#5278) --- aspnetcore/fundamentals/configuration/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)] From b4292ee7f1330b662d1325cf7ce9ab7921aaf3fd Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Fri, 26 Jan 2018 14:52:24 -0600 Subject: [PATCH 2/2] Response Caching Middleware topic: Convert sample to RP + UE pass (#5277) --- aspnetcore/performance/caching/middleware.md | 74 +++--- .../caching/middleware/sample/.bowerrc | 3 + .../caching/middleware/sample/.gitignore | 217 ++++++++++++++++++ .../middleware/sample/Pages/Index.cshtml | 23 ++ .../middleware/sample/Pages/Index.cshtml.cs | 11 + .../middleware/sample/Pages/_Layout.cshtml | 70 ++++++ .../sample/Pages/_ViewImports.cshtml | 3 + .../middleware/sample/Pages/_ViewStart.cshtml | 3 + .../caching/middleware/sample/Program.cs | 18 ++ .../caching/middleware/sample/README.md | 7 + .../ResponseCachingMiddleware.csproj} | 2 +- .../{samples/1.x => sample}/Startup.cs | 37 ++- .../sample/appsettings.Development.json | 10 + .../sample/appsettings.Production.json | 10 + .../middleware/sample/appsettings.json | 8 + .../caching/middleware/sample/bower.json | 10 + .../middleware/sample/bundleconfig.json | 24 ++ .../middleware/sample/wwwroot/css/site.css | 25 ++ .../sample/wwwroot/css/site.min.css | 1 + .../middleware/sample/wwwroot/favicon.ico | Bin 0 -> 32038 bytes .../middleware/sample/wwwroot/js/site.js | 1 + .../middleware/sample/wwwroot/js/site.min.js | 0 .../caching/middleware/samples/1.x/README.md | 7 - .../samples/1.x/ResponseCachingSample.csproj | 15 -- .../caching/middleware/samples/2.x/Program.cs | 46 ---- .../caching/middleware/samples/2.x/README.md | 7 - 26 files changed, 493 insertions(+), 139 deletions(-) create mode 100644 aspnetcore/performance/caching/middleware/sample/.bowerrc create mode 100644 aspnetcore/performance/caching/middleware/sample/.gitignore create mode 100644 aspnetcore/performance/caching/middleware/sample/Pages/Index.cshtml create mode 100644 aspnetcore/performance/caching/middleware/sample/Pages/Index.cshtml.cs create mode 100644 aspnetcore/performance/caching/middleware/sample/Pages/_Layout.cshtml create mode 100644 aspnetcore/performance/caching/middleware/sample/Pages/_ViewImports.cshtml create mode 100644 aspnetcore/performance/caching/middleware/sample/Pages/_ViewStart.cshtml create mode 100644 aspnetcore/performance/caching/middleware/sample/Program.cs create mode 100644 aspnetcore/performance/caching/middleware/sample/README.md rename aspnetcore/performance/caching/middleware/{samples/2.x/ResponseCachingSample.csproj => sample/ResponseCachingMiddleware.csproj} (78%) rename aspnetcore/performance/caching/middleware/{samples/1.x => sample}/Startup.cs (53%) create mode 100644 aspnetcore/performance/caching/middleware/sample/appsettings.Development.json create mode 100644 aspnetcore/performance/caching/middleware/sample/appsettings.Production.json create mode 100644 aspnetcore/performance/caching/middleware/sample/appsettings.json create mode 100644 aspnetcore/performance/caching/middleware/sample/bower.json create mode 100644 aspnetcore/performance/caching/middleware/sample/bundleconfig.json create mode 100644 aspnetcore/performance/caching/middleware/sample/wwwroot/css/site.css create mode 100644 aspnetcore/performance/caching/middleware/sample/wwwroot/css/site.min.css create mode 100644 aspnetcore/performance/caching/middleware/sample/wwwroot/favicon.ico create mode 100644 aspnetcore/performance/caching/middleware/sample/wwwroot/js/site.js create mode 100644 aspnetcore/performance/caching/middleware/sample/wwwroot/js/site.min.js delete mode 100644 aspnetcore/performance/caching/middleware/samples/1.x/README.md delete mode 100644 aspnetcore/performance/caching/middleware/samples/1.x/ResponseCachingSample.csproj delete mode 100644 aspnetcore/performance/caching/middleware/samples/2.x/Program.cs delete mode 100644 aspnetcore/performance/caching/middleware/samples/2.x/README.md 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.
(}(2Kf7BCZ>Vc;V>ccbk2~NGaf6wGQH@W9&?Zt3v(h*P4xDrN>ex7+jH*+Qg z%^jH$&+*!v{sQ!xkWN4+>|b}qGvEd6ANzgqoVy5Qfws}ef2QqF{iiR5{pT}PS&yjo z>lron#va-p=v;m>WB+XVz|o;UJFdjo5_!RRD|6W{4}A2a#bZv)gS_`b|KsSH)Sd_JIr%<%n06TX&t{&!H#{)?4W9hlJ`R1>FyugOh3=D_{einr zu(Wf`qTkvED+gEULO0I*Hs%f;&=`=X4;N8Ovf28x$A*11`dmfy2=$+PNqX>XcG`h% zJY&A6@&)*WT^rC(Caj}2+|X|6cICm5h0OK0cGB_!wEKFZJU)OQ+TZ1q2bTx9hxnq& z$9ee|f9|0M^)#E&Pr4)f?o&DMM4w>Ksb{hF(0|wh+5_{vPow{V%TFzU2za&gjttNi zIyR9qA56dX52Qbv2aY^g`U7R43-p`#sO1A=K S2aKgfR+Yu^bQ*i-qu z%0mP;Ap)B~zZgO9lG^`325gOf?iUHF{~7jyGC)3L(eL(SQ70VzR~wLN18tnx(Cz2~ zctBl1kI)wAe+cxWHw*NW-d;=pd+>+wd$a@GBju*wFvabSaPtHiT!o#QFC+wBVwYo3s=y;z1jM+M=Fj!FZM>UzpL-eZzOT( zhmZmEfWa=%KE#V3-ZK5#v!Hz d{zc^{ctF~- z>DT-U`}5!fk$aj24`#uGdB7r`>oX5tU|d*b|N3V1lXmv%MGrvE(dXG)^-J*LA>$LE z7kut4`zE)v{@Op|(|@i#c>tM!12FQh?}PfA0`Bp%=%*RiXVzLDXnXtE@4B)5uR}a> zbNU}q+712pIrM`k^odG8dKtG$zwHmQI^c}tfjx5?egx3!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