Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable ASP.net Cache

Is there a way to disable asp.net caching on selected page. It would be nice if this can be done from the web.config.

like image 695
Antony Delaney Avatar asked Jul 05 '10 15:07

Antony Delaney


3 Answers

<!-- In the page itself -->
<%@ OutputCache Location="None" %>

Or

// In the code-behind
Response.Cache.SetCacheability(HttpCacheability.NoCache)

Unfortunately, it has to be done within the page. There's no easy way to do it from web.config. For more information, check out:

MSDN - Setting the Cacheability of a Page

like image 85
Justin Niessner Avatar answered Nov 03 '22 03:11

Justin Niessner


Yes you can if you are willing to create your own config section: http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

In your config section put something like,

<cachingConfig>
    <path>/Navigation/Menu.aspx</path>
    <path>/Target/Console.aspx</path>
    <path>/Target/Charting/Chart.aspx</path>
</cachingConfig>

You could add more properties such as duration if you like.

Then, on the page_Init method of your pages, check this configuration section and call the following where appropriate:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Edit: Tip: Put the init code in a base class that your pages inherit, so that it is only one place.

like image 44
Daniel Dyson Avatar answered Nov 03 '22 03:11

Daniel Dyson


Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
like image 7
Samdrain Avatar answered Nov 03 '22 03:11

Samdrain