what is the different between the two lines below? :
Response.Cache.SetCacheability(HttpCacheability.NoCache);
and
Response.CacheControl = "no-cache";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you read through the documentation, generally, there isn’t any difference at all.
However, since the former uses the Cache object (
HttpCachePolicyBasein 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
NoCacheneeds 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.Cacheobject than to code the headers directly (it really comes into its own for ETags and If-Not-Modified-Since handling, for example).