ResponseCache attribute does not cache client-side data

In an ASP.NET Core application, I have an action method that returns some data. I wanted to cache this data on the client side . Therefore, based on the documentation here, I can use the ResponseCache attribute for the action method. This attribute adds a Cache-Control header to the response.

Response caching refers to setting the headers associated with the cache over HTTP responses made by ASP.NET Core MVC actions. These headers indicate how you want client and intermediate (proxy) machines to cache responses to specific requests (if at all). This can reduce the number of requests the client or proxy server requests for the web server, since in the future requests for the same action may be sent from the client or proxy cache.

also

Response caching does not cache responses on the web server. This is different from output caching, which will cache in-memory responses to the server in earlier versions of ASP.NET and ASP.NET MVC.

So this is what my action method looks like

 public class LookupController : Controller { [HttpGet] [ResponseCache(Duration = 120)] public IEnumerable<StateProvinceLookupModel> GetStateProvinces() { return _domain.GetStateProvinces(); } } 

Then I call the method using the browser as http: // localhost: 40004 / lookup / getstateprovinces Here are the request and response headers

enter image description here

Note that the response headers have Cache-Control: public,max-age-120 , as expected. However, if you refresh the page using F5 (up to 120 seconds), then the debugger breakpoint inside the GetStateProvince action method will always be a hit. This means that it does not transmit data on the client side.

Is there anything else I need to do to enable client side caching?

Update I tried using IE, Chrome and POSTMAN with no luck. Each time I type a URL in the address bar or delete an update, the client (i.e. the browser or postman) makes a call to action method.

+15
asp.net-core asp.net-core-mvc coreclr
source share
3 answers

Actually, the ResponseCache attribute works as intended.
The difference is that the response is cached if you navigate the pages of your website ( case 1 ) or use the back and forward buttons ( not when refreshing the page ).

As an example of case 1, I have the following:

As you will see in the article " Caching Responses in ASP.Net Core 1.1" , the following is indicated:

During a browser session, when viewing multiple pages on a website or using the back and forward buttons to visit pages, content will be served from the browser’s local cache (if not expired).
But when the page is updated through F5 , the request is sent to the server, and the contents of the page are updated. You can verify this by updating the contact page using F5.
Therefore, when you press F5, the response caching expiration value does not play a role in serving the content. You should see 200 response to a contact request.

Recommendations:
[one]. ASP.NET Kernel Response Caching Example
[2]. ResponseCache attribute example
[3]: How to control web page caching in all browsers?

+11
source share

In short, using a ResponseCache attribute like the following is enough to get client-side expiration caching to work in a completely new default dotnet base project (including async methods):

 [HttpGet] [ResponseCache(Duration = 120)] public IEnumerable<StateProvinceLookupModel> GetStateProvinces() { return _domain.GetStateProvinces(); } 

This works correctly in the screenshot above, since Cache-Control: public,max-age=120 is visible there. In most cases, browsers will not send subsequent requests until the expiration date (i.e. within the next 120 seconds or 2 minutes), but this is the solution of the browser (or another client).

If the request is sent independently, you either have middleware or a server configuration that overwrites the response headers, or your client ignores the caching directive. In the screenshot above, the client ignores caching because it contains the header of the cache control.

Common cases where the client cache is ignored and the request is sent:

  • Chrome prevents any caching when using HTTPS without a certificate (or an invalid certificate, this is common with local development, so be sure to use HTTP when testing the cache or trust a self-signed certificate)
  • Most browser developer tools disable caching by default when opened, this can be disabled
    • Browsers usually send extra headers, Chrome sends Cache-Control: no-cache
  • Updating directly (i.e. Ctrl + F5) will cause most browsers not to use the cache and make a request regardless of age
    • Browsers usually send extra headers, Chrome sends Cache-Control: max-age=0 (this can be seen in the screenshot)
  • The postman sends the Cache-Control: no-cache header Cache-Control: no-cache which allows you to bypass the local cache, which leads to sending requests; You can disable it in the settings dialog box, in which case requests will no longer be sent with the above client cache configuration

At the moment, we have gone beyond the expiration-based client caching, and the server will somehow receive a request, and a different level of caching will occur: you can force the server to respond with 304 Not Modified code (which then works again for the client to interpret in any way, which he wants) or use the server-side cache and respond with full content. Or you can’t use any subsequent caching and just do all the request processing again on the server.


Note. The ResponseCache attribute should not be confused with services.AddResponseCaching() & app.UseResponseCaching() middleware in the startup configuration, as it is designed for server-side caching (which by default uses a cache in memory when using middleware).). Middleware is not required for client caching, the attribute alone is sufficient.

0
source share

First of all, I want to clarify something, and I'm sure you already knew that.

  • ResponseCache is not equal to OutputCache.

  • ResponseCache matches my heading of thinking, but it does not cache anything on the server side.

Now, if you want the cache to be the same as OutputCache, you may have to use version 1.1 for the release that is just coming out.

Preview version of ASP.NET core 1.1

https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/

They are introducing new response caching middleware. Middleware Response Caching

A demonstration of this is available here. https://github.com/aspnet/ResponseCaching/blob/dev/samples/ResponseCachingSample/Startup.cs

-one
source share

All Articles