Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Cache-Control: no-cache in HTTP Response header?

Net 4 and C#.

I would need set send to Browser Cache-Control (Cache-Control: no-cache) in the HTTP Response header for a Web Form page.

Any idea how to do it?

Thanks for your time.

like image 704
GibboK Avatar asked Aug 30 '11 11:08

GibboK


People also ask

How do I set Cache-Control no-cache in response header?

To set the header to public, private or no-cache, use the Response. Cache. SetCacheability method. Response.

What happens if there is no-Cache-Control header?

Without the cache control header the browser requests the resource every time it loads a new(?) page.

How do I add Cache-Control to my header?

If you want to enable Cache-Control for all files, add Header set line outside the filesMatch block. As you can see, we set the Cache-Control header's max-age to 3600 seconds and to public for the listed files.

What is the use of Cache-Control in HTTP response header?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).


2 Answers

Try this:

Response.AppendHeader("Cache-Control", "no-cache");

However, you should know that this header alone won't give you a reliable cross-browser way to prevent caching. See this answer for more accurate solution: Making sure a web page is not cached, across all browsers

like image 173
Dyppl Avatar answered Sep 23 '22 17:09

Dyppl


In MVC you can set it in the Controller class, so the View not use cache;

public ActionResult User()
{
    Response.CacheControl = "no-cache";
    return View();
}
like image 41
Hernaldo Gonzalez Avatar answered Sep 23 '22 17:09

Hernaldo Gonzalez