2

what is the different between the two lines below? :

Response.Cache.SetCacheability(HttpCacheability.NoCache);

and

 Response.CacheControl = "no-cache";
Salahaldin
  • 92
  • 4
  • 15

2 Answers2

5

If you read through the documentation, generally, there isn't any difference at all.

However, since the former uses the Cache object (HttpCachePolicyBase in Asp.Net 4 or Asp.Net MVC) it provides:

a) An abstraction over the response object's caching policy that can be useful in testing scenarios

b) Potential future-proofing if the HTTP spec is extended for some reason to mean that NoCache needs more headers in the response, or other tweaks to the response.

It also allows the notion of 'non-cacheability' to be adapted according to the capabilities of the client transparently; which is important in the case that HTTP does evolve - but also already in the case of HTTP 1.0 and 1.1 clients (differences between 1.0 and 1.1 are well-summarised in HTTP 1.0 vs 1.1).

In the second line, you are taking responsibility for how the no-cache directive is applied (using a somewhat magic string) - granted, it's still testable, but if the HTTP spec does change then you have to code around it yourself.

Ultimately, it's better design to use the Response.Cache object than to code the headers directly (it really comes into its own for ETags and If-Not-Modified-Since handling, for example).

Community
  • 1
  • 1
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

The Response.CacheControl property is only maintained for backwards compatibility. When you assign a string to it, it will set the corresponding value in the Response.Cache object.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005