{"id":70695,"date":"2022-05-28T14:17:01","date_gmt":"2022-05-28T12:17:01","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=70236"},"modified":"2023-03-07T05:25:54","modified_gmt":"2023-03-07T04:25:54","slug":"aspnetcore-response-caching","status":"publish","type":"post","link":"https:\/\/code-maze.com\/aspnetcore-response-caching\/","title":{"rendered":"Response Caching in ASP.NET Core"},"content":{"rendered":"<p>In this article, we are going to discuss how response caching works in ASP.NET Core.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/aspnetcore-features\/ResponseCaching\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s get started.<\/p>\n<h2>What is Response Caching?<\/h2>\n<p>Response Caching is the mechanism of caching a server&#8217;s response by a browser or other clients. This will help in serving future requests for the same resources very quickly. Additionally, this will free up the server from processing and generating the same response multiple times.<\/p>\n<p>ASP.NET Core uses the <code>ResponseCache<\/code> attribute to set the response caching headers. Furthermore, we can use the <code>Response Caching Middleware<\/code> to control the caching behavior from the server-side. Once we set the response caching headers, clients and other proxies can read those to determine how to cache the response from the server. <strong>As per the HTTP 1.1 Response Cache Specification, browsers, clients and proxies should conform to the caching headers<\/strong>.\u00a0<\/p>\n<h2>HTTP Based Response Caching<\/h2>\n<p>Now, let&#8217;s talk about different <code>HTTP Cache Directives<\/code> and how we can control the caching behavior using those directives.<\/p>\n<p>The <code>cache-control<\/code> is the primary header field that we use to specify the way response can be cached. <strong><span data-preserver-spaces=\"true\">When the cache-control header is present in the response, browsers, clients, and proxy servers should honor the headers and comply with them.<\/span><\/strong><\/p>\n<p>So, let&#8217;s inspect the common <code>cache-control<\/code> directives:<\/p>\n<ul>\n<li><strong>public<\/strong> &#8211; indicates that a cache can store the response either at the client-side or at a shared location<\/li>\n<li><strong>private<\/strong> &#8211; <span data-preserver-spaces=\"true\">indicates that only a private cache on the client-side may store the response, but not a shared cache<\/span><\/li>\n<li><strong>no-cache<\/strong> &#8211; specifies that a cache must not use a stored response for any requests<\/li>\n<li><strong>no-store<\/strong> &#8211; indicates that a cache must not store the response<\/li>\n<\/ul>\n<p><strong>no-cache<\/strong> and <strong>no-store<\/strong> may sound similar and even behave similarly, but there are some differences in the way browsers or clients understand both. We are going to discuss this in detail while working on the examples.<\/p>\n<p>Apart from the <strong>cache-control<\/strong>, there are a few other headers that can control the caching behavior:<\/p>\n<p>The <strong>pragma<\/strong> header is for backward compatibility with <strong>HTTP 1.0 specification<\/strong> and the <strong>no-cache<\/strong> directive. If we specify the <strong>cache-control<\/strong> header, it will ignore the <strong>pragma<\/strong> header.<\/p>\n<p><strong>Vary<\/strong> &#8211;<span data-preserver-spaces=\"true\"> indicates that it can send a cached response only if all the fields that we provide in the header match exactly with the previous request. If any of the fields changes, the server generates a new response.<\/span><\/p>\n<h3>HTTP Cache Directive Examples<\/h3>\n<p>Now, we are going to create an ASP.NET Core application and see the cache directives in action. Let&#8217;s create an ASP.NET Core Web API project and add a controller action method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Route(\"api\/[controller]\")]\r\n[ApiController]\r\npublic class ValuesController : ControllerBase\r\n{\r\n    [HttpGet]        \r\n    public IActionResult Get()\r\n    {\r\n        return Ok($\"Response was generated at {DateTime.Now}\");\r\n    }\r\n}<\/pre>\n<p>Every time we execute this endpoint, we can observe that it appends the actual date and time of execution to the response. Now we are going to add response caching to the endpoint.<\/p>\n<p><strong>Important note: <\/strong>While testing response caching we should always click links on the web page or use swagger to execute the API endpoints in the browser. Otherwise, if we try to refresh the page or invoke the URI again, the browser will always request a new response from the server irrespective of the response cache settings.<\/p>\n<h2>ResponseCache Attribute<\/h2>\n<p>For an ASP.NET Core application, the <code>ResponseCache<\/code> attribute defines the properties for setting the appropriate response caching headers. We can apply this attribute either at the controller level or for individual endpoints.<\/p>\n<p>Let&#8217;s add the <code>ResponseCache<\/code> attribute to the API endpoint:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"1\">[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Any)]\r\npublic IActionResult Get()\r\n{\r\n    return Ok($\"Response was generated at {DateTime.Now}\");\r\n}<\/pre>\n<p>This <code>Duration<\/code> property will produce the <code>max-age<\/code> header, which we use to set the cache duration for 2 minutes (120 seconds). Similarly, the <code>Location<\/code> property will set the location in the <code>cache-control<\/code> header. Since we set the location as <code>Any<\/code>, both the client and server will be able to cache the response, which is equivalent to the <code>public<\/code> directive of the <code>cache-control<\/code> header.<\/p>\n<p>So, let&#8217;s invoke the API endpoint and verify these in the response headers:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cache-control: public,max-age=120<\/code><\/p>\n<p>Additionally, when we invoke the endpoints multiple times, whenever the browser uses a cached response, it will indicate in the status code that the response is taken from the disk cache:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Status Code: 200 (from disk cache)<\/code><\/p>\n<p>Now let&#8217;s explore the different options for the <code>ResponseCache<\/code> parameters.<\/p>\n<p>For changing the cache location to <code>private<\/code>, we just need to change the <code>Location<\/code> property&#8217;s value to <code>ResponseCacheLocation.Client<\/code>:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Client)]<\/code><\/p>\n<p>This will change the <code>cache-control<\/code> header value to <code>private<\/code> which means that only the client can cache the response:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cache-control: private,max-age=120<\/code><\/p>\n<p>Now let&#8217;s update the <code>Location<\/code> parameter to <code>ResponseCacheLocation.None<\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[ResponseCache(Duration = 120, Location = ResponseCacheLocation.None)]<\/code><\/p>\n<p>This will set both the <code>cache-control<\/code> and <code>pragma<\/code> header to <code>no-cache<\/code>, which means the client cannot use a cached response without revalidating with the server:\u00a0<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cache-control: no-cache,max-age=120\r\npragma: no-cache<\/pre>\n<p>In this configuration, we can verify that the browser does not use the cached response and the server generates a new response every time.<\/p>\n<div style=\"padding: 20px; border-left: 5px gray solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">We also have articles on <a href=\"https:\/\/code-maze.com\/aspnetcore-distributed-caching\/\" target=\"_blank\" rel=\"noopener\">Distributed Caching<\/a> and <a href=\"https:\/\/code-maze.com\/aspnetcore-in-memory-caching\/\" target=\"_blank\" rel=\"noopener\">In-Memory Caching<\/a>. So, if you like to learn more about the caching topic, feel free to visit those articles.<\/div>\n<h2>NoStore Property<\/h2>\n<p>Now let&#8217;s set the <code>NoStore<\/code> property of the <code>ResponseCache<\/code> attribute to <code>true<\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Any, NoStore = true)]<\/code><\/p>\n<p>This will set the <code>cache-control<\/code> response header to <code>no-store<\/code> which indicates that the client should not cache the response:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cache-control: no-store<\/code><\/p>\n<p>Notice that this will override the value that we set for <code>Location<\/code>. In this case, as well, the client will not cache the response.<\/p>\n<p>Even though the <code>no-cache<\/code> and <code>no-store<\/code> values for <code>cache-control<\/code> may give the same results while testing, <strong>the browsers, clients, and proxies interpret these headers differently.<\/strong> While <code>no-store<\/code> indicates that the clients or proxies should not store the response or any part of it anywhere, <code>no-cache<\/code> just means that the client should not use a cached response without revalidating with the server.\u00a0<\/p>\n<p>Now let&#8217;s see how the <code>vary<\/code> header works.<\/p>\n<h2>VaryByHeader Property<\/h2>\n<p>For setting the <code>vary<\/code> header, we can use the <code>VaryByHeader<\/code> property of the <code>ResponseCache<\/code> attribute:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Any, VaryByHeader = \"User-Agent\")]<\/code><\/p>\n<p>Here we set the value for the <code>VaryByHeader<\/code> property to <code>User-Agent<\/code>, which will use the cached response as long as the request comes from the same client device. Once the client device changes, the <code>User-Agent<\/code> value will be different and it will fetch a new response from the server. Let&#8217;s verify this.<\/p>\n<p>First, let&#8217;s check the presence of the <code>vary<\/code> header in the response headers:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">vary: User-Agent<\/code><\/p>\n<p>Additionally, on inspecting the request headers, let&#8217;s verify the <code>user-agent<\/code> header, which corresponds to the device and browser that we use:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">user-agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/101.0.4951.67 Safari\/537.36<\/code><\/p>\n<p>For testing purposes, let&#8217;s simulate a new request coming from a different device. For that, we can use the <strong>toggle device <\/strong>feature available with the developer tools of browsers such as Chrome and Edge:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/device-toggle-toolbar.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-70696 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/device-toggle-toolbar.png\" alt=\"device toggle toolbar for response caching\" width=\"168\" height=\"133\" \/><\/a><\/p>\n<p>After clicking on the toggle device button in the developer tools, we can choose the device that we want to simulate in the browser:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/choose-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-70698\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/choose-device-300x284.png\" alt=\"choose device for response caching\" width=\"300\" height=\"284\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/choose-device-300x284.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2022\/05\/choose-device.png 346w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This will help us switch the browser to a phone or tablet simulation mode.<\/p>\n<p>Once we switch the device, we can see that the server abandons the cached response and sends a new one. Also, note that the <code>user-agent<\/code> header will be different:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">user-agent: Mozilla\/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit\/605.1.15 (KHTML, like Gecko) Version\/13.0.3 Mobile\/15E148 Safari\/604.1<\/code><\/p>\n<p>So by setting the <code>VaryByHeader<\/code> property as <code>User-Agent<\/code>, we can make the server send a new response for a new device.<\/p>\n<h2>VaryByQueryKeys Property<\/h2>\n<p>By using the <code>VaryByQueryKeys<\/code> property of the <code>ResponseCache<\/code> attribute, we can make the server send a new response when the specified query string parameters change. Of course, by specifying the value as &#8220;*&#8221;, we can generate a new response when any of the query string parameters changes.<\/p>\n<p>For example, we might want to generate a new response when the <code>Id<\/code> value changes in the URI:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">...\/values?id=1\u00a0\r\n...\/values?id=2<\/pre>\n<p>For this, let&#8217;s modify the <code>Get<\/code> action to include the <code>id<\/code> parameter and provide the <code>VaryByQueryKeys<\/code> property for the <code>ResponseCache<\/code> attribute:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpGet]\r\n[ResponseCache(Duration = 120, Location = ResponseCacheLocation.Any, VaryByQueryKeys = new string[] { \"id\" })]\r\npublic IActionResult Get(int id)\r\n{\r\n    return Ok($\"Response was generated for Id:{id} at {DateTime.Now}\");\r\n}<\/pre>\n<p>Remember that we need to enable the <code>Response Caching Middleware<\/code> for setting the <code>VaryByQueryKeys<\/code> property. Otherwise, the code will throw a runtime exception.<\/p>\n<h2>Response Caching Middleware\u00a0<\/h2>\n<p>The Response Caching Middleware in ASP.NET Core app determines when a response can be cached, and stores and serves the response from the cache.\u00a0<\/p>\n<p>For enabling the Response Caching Middleware, we just need to add a couple of lines of code in the <code>Program<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4,9\">var builder = WebApplication.CreateBuilder(args);\r\n\r\nbuilder.Services.AddControllers();\r\nbuilder.Services.AddResponseCaching();\r\n...\r\nvar app = builder.Build();\r\n...\r\napp.MapControllers();\r\napp.UseResponseCaching();\r\n\r\napp.Run();\r\n<\/pre>\n<p>First, we need to add the middleware using the <code>AddResponseCaching()<\/code> method and then we can configure the app to use the middleware with the <code>UseResponseCaching()<\/code> method.<\/p>\n<p>That&#8217;s it. We have enabled the Response Caching Middleware and the\u00a0 <code>VaryByQueryKeys<\/code> property should work now.<\/p>\n<p>Let&#8217;s run the application and navigate to the <code>\/<span class=\"enlighter-text\">values?id=<\/span><span class=\"enlighter-n1\">1<\/span><\/code> endpoint:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Response was generated for Id:1 at 23-05-2022 12:07:22<\/code><\/p>\n<p>We can see that we&#8217;ll receive a cached response as long as the query string is the same, but once we change the query string, the server will send a new response.<\/p>\n<p>Let&#8217;s change the query string to <code>\/<span class=\"enlighter-text\">values?id=2<\/span><\/code>:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Response was generated for Id:2 at 23-05-2022 12:07:32<\/code><\/p>\n<p><strong>Note that there is no corresponding HTTP header for the <code>VaryByQueryKeys<\/code> property. This property is an HTTP feature that is handled by Response Caching Middleware<\/strong>.<\/p>\n<p>With that, we have covered all the common HTTP cache directives.<\/p>\n<h2>Cache Profiles<\/h2>\n<p>Instead of duplicating the response cache settings on individual controller action methods, we can configure cache profiles and reuse them across the application. Once we set up a cache profile, the application can use its values as defaults for the <code>ResponseCache<\/code> attribute. Of course, we can override the defaults by specifying the properties on the attribute.<\/p>\n<p>We can define a cache profile in the <code>Program<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7-12\">using Microsoft.AspNetCore.Mvc;\r\n\r\nvar builder = WebApplication.CreateBuilder(args);\r\n\r\nbuilder.Services.AddControllers(options =&gt;\r\n{\r\n    options.CacheProfiles.Add(\"Cache2Mins\",\r\n        new CacheProfile()\r\n        {\r\n            Duration = 120,\r\n            Location =  ResponseCacheLocation.Any            \r\n        });\r\n});\r\n...<\/pre>\n<p>Here we define a new cache profile called <code>Cache2Mins<\/code> with a duration of 2 minutes and location as <code>public<\/code>.<\/p>\n<p>Now we can apply this cache profile to any controller or endpoint:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"2\">[HttpGet]\r\n[ResponseCache(CacheProfileName = \"Cache2Mins\")]\r\npublic IActionResult Get()\r\n{\r\n    return Ok($\"Response was generated at {DateTime.Now}\");\r\n}<\/pre>\n<p>This will apply the defined <code>cache-control<\/code> settings for the response:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cache-control: public,max-age=120<\/code><\/p>\n<p>Instead of hard-coding the cache settings in the <code>Program<\/code> class, we can define multiple cache profiles in the <code>appsettings<\/code> file and make the response cache settings configurable:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-highlight=\"5-10\">{\r\n  \"Logging\": {\r\n...\r\n  },\r\n  \"CacheProfiles\": {\r\n    \"Cache2Mins\": {\r\n      \"Duration\": 120,\r\n      \"Location\": \"Any\"\r\n    }\r\n  },\r\n  \"AllowedHosts\": \"*\"\r\n}<\/pre>\n<p>Then, we can read the cache profiles in the <code>Program<\/code> class using the <code>ConfigurationManager<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7-9, 11-16\">using Microsoft.AspNetCore.Mvc;\r\n\r\nvar builder = WebApplication.CreateBuilder(args);\r\n\r\nbuilder.Services.AddControllers(options =&gt;\r\n{\r\n    var cacheProfiles = builder.Configuration\r\n            .GetSection(\"CacheProfiles\")\r\n            .GetChildren();\r\n\r\n    foreach (var cacheProfile in cacheProfiles)\r\n    {\r\n        options.CacheProfiles\r\n        .Add(cacheProfile.Key, \r\n        cacheProfile.Get&lt;CacheProfile&gt;());\r\n    }\r\n});<\/pre>\n<p>This is a much better way of defining cache profiles, especially when we need to define multiple cache profiles for our application.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we discussed what response caching is and how it works in ASP.NET Core applications. Additionally, we have learned how to control the response caching behavior by setting the cache directives and using the response caching middleware. Finally, we learned how to define a cache profile and reuse it across the application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to discuss how response caching works in ASP.NET Core. Let&#8217;s get started. What is Response Caching? Response Caching is the mechanism of caching a server&#8217;s response by a browser or other clients. This will help in serving future requests for the same resources very quickly. Additionally, this will free [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":62187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1677,171],"tags":[1265,1266,1267,1268,1269],"class_list":["post-70695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-caching","category-http","tag-response-caching-in-asp-net-core","tag-response-caching-middleware","tag-responsecache-attribute","tag-varybyheader","tag-varybyquerykeys","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Response Caching in ASP.NET Core - Code Maze<\/title>\n<meta name=\"description\" content=\"Response Caching is the mechanism of caching a server&#039;s response for future requests by a browser or other clients.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Response Caching in ASP.NET Core - Code Maze\" \/>\n<meta property=\"og:description\" content=\"Response Caching is the mechanism of caching a server&#039;s response for future requests by a browser or other clients.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-28T12:17:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-07T04:25:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Muhammed Saleem\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Muhammed Saleem\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\"},\"author\":{\"name\":\"Muhammed Saleem\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/590295cbd99c624d65d15016edeefaf2\"},\"headline\":\"Response Caching in ASP.NET Core\",\"datePublished\":\"2022-05-28T12:17:01+00:00\",\"dateModified\":\"2023-03-07T04:25:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\"},\"wordCount\":1629,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"keywords\":[\"response caching in asp.net core\",\"response caching middleware\",\"ResponseCache attribute\",\"VaryByHeader\",\"VaryByQueryKeys\"],\"articleSection\":[\"Caching\",\"HTTP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\",\"url\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\",\"name\":\"Response Caching in ASP.NET Core - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"datePublished\":\"2022-05-28T12:17:01+00:00\",\"dateModified\":\"2023-03-07T04:25:54+00:00\",\"description\":\"Response Caching is the mechanism of caching a server's response for future requests by a browser or other clients.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/aspnetcore-response-caching\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png\",\"width\":1100,\"height\":620,\"caption\":\"ASP.NET Core\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/aspnetcore-response-caching\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Response Caching in ASP.NET Core\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/590295cbd99c624d65d15016edeefaf2\",\"name\":\"Muhammed Saleem\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Muhammed-Saleem-400-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Muhammed-Saleem-400-150x150.png\",\"caption\":\"Muhammed Saleem\"},\"description\":\"Muhammed Saleem has 16 years of proven track record in architecting, designing &amp; developing high-quality software solutions. He's a problem solver at heart and passionate about building great software. He's a curious, self-driven learner and self-starter with a strong base of computer science fundamentals. He has experience in implementing best practices for managing software development, ensuring code quality, Application Lifecycle Management, DevOps, etc.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/muhammedsaleem\/\"],\"url\":\"https:\/\/code-maze.com\/author\/muhammed-saleem\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Response Caching in ASP.NET Core - Code Maze","description":"Response Caching is the mechanism of caching a server's response for future requests by a browser or other clients.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/aspnetcore-response-caching\/","og_locale":"en_US","og_type":"article","og_title":"Response Caching in ASP.NET Core - Code Maze","og_description":"Response Caching is the mechanism of caching a server's response for future requests by a browser or other clients.","og_url":"https:\/\/code-maze.com\/aspnetcore-response-caching\/","og_site_name":"Code Maze","article_published_time":"2022-05-28T12:17:01+00:00","article_modified_time":"2023-03-07T04:25:54+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","type":"image\/png"}],"author":"Muhammed Saleem","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Muhammed Saleem","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/"},"author":{"name":"Muhammed Saleem","@id":"https:\/\/code-maze.com\/#\/schema\/person\/590295cbd99c624d65d15016edeefaf2"},"headline":"Response Caching in ASP.NET Core","datePublished":"2022-05-28T12:17:01+00:00","dateModified":"2023-03-07T04:25:54+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/"},"wordCount":1629,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","keywords":["response caching in asp.net core","response caching middleware","ResponseCache attribute","VaryByHeader","VaryByQueryKeys"],"articleSection":["Caching","HTTP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/aspnetcore-response-caching\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/","url":"https:\/\/code-maze.com\/aspnetcore-response-caching\/","name":"Response Caching in ASP.NET Core - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","datePublished":"2022-05-28T12:17:01+00:00","dateModified":"2023-03-07T04:25:54+00:00","description":"Response Caching is the mechanism of caching a server's response for future requests by a browser or other clients.","breadcrumb":{"@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/aspnetcore-response-caching\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-aspnetcore.png","width":1100,"height":620,"caption":"ASP.NET Core"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/aspnetcore-response-caching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Response Caching in ASP.NET Core"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/590295cbd99c624d65d15016edeefaf2","name":"Muhammed Saleem","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Muhammed-Saleem-400-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2024\/01\/Muhammed-Saleem-400-150x150.png","caption":"Muhammed Saleem"},"description":"Muhammed Saleem has 16 years of proven track record in architecting, designing &amp; developing high-quality software solutions. He's a problem solver at heart and passionate about building great software. He's a curious, self-driven learner and self-starter with a strong base of computer science fundamentals. He has experience in implementing best practices for managing software development, ensuring code quality, Application Lifecycle Management, DevOps, etc.","sameAs":["https:\/\/www.linkedin.com\/in\/muhammedsaleem\/"],"url":"https:\/\/code-maze.com\/author\/muhammed-saleem\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=70695"}],"version-history":[{"count":4,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70695\/revisions"}],"predecessor-version":[{"id":70701,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70695\/revisions\/70701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62187"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=70695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=70695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=70695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}