5

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?

VoronoiPotato
  • 3,113
  • 20
  • 30
harishr
  • 17,807
  • 9
  • 78
  • 125

1 Answers1

9

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).

Stefan Ossendorf
  • 696
  • 6
  • 14
  • 4
    Note for .NET Core development, override `OnActionExecuted` function and place `Response.Headers.Add("Cache-Control", "no-cache");` in it. Remember to leave the `base.OnActionExecuted(context);` in that function to ensure that it handles the commands properly. – Grungondola Jun 01 '18 at 12:09