Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable caching of Web API responses

I want to disable browser caching of all the Web API responses across all the clients. Even though I can use libraries like CacheOutput or CacheCow as suggested in Scott Hanselman's blog but my requirement is not that complex. I just want to disable caching of all the Web API responses and do not need any custom control over it.

  • How do I do that in ASP.NET Web API 2?

  • Which headers do I need to set? 'Cache-Control' : 'no-cache'?

  • Is ETag, Last-Modified, etc needed? Or maybe any other response headers??

  • It need to be implemented in a DelegatingHandler, right?

like image 832
harishr Avatar asked Feb 05 '15 04:02

harishr


People also ask

How do I disable cache in API?

Just use the Cache-Control: no-cache header. Implement it as delegating-Handler and make sure your header is applied (with MS Owin Implementation hook up on OnSendingHeaders() .

Should you cache API responses?

Caches along the response path can take a copy of a response, but only if the caching metadata allows them to do so. Optimizing the network using caching improves the overall quality-of-service in the following ways: Reduce bandwidth. Reduce latency.

How do I stop browser caching request?

Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache. You can then close out of Developer Tools. Bear in mind that this "Disable cache" will affect every single web page you browse.


1 Answers

Just use the Cache-Control: no-cache header.
Implement it as delegating-Handler and make sure your header is applied (with MS Owin Implementation hook up on OnSendingHeaders(). I'm using it here OnSendingHeaders() Example).

like image 128
Stefan Ossendorf Avatar answered Oct 05 '22 23:10

Stefan Ossendorf