c# - Enable Cache Globally from Global.asax -


i trying enable server-side cache (output caching) globally global.asax in mvc3 project.

i tried this:

protected void application_beginrequest(object sender, eventargs e)     {         if (httpcontext.current.request.path.contains("private"))         {             return;         }         response.cache.setexpires(datetime.now.addseconds(300));         response.cache.setcacheability(httpcacheability.server);         response.cache.varybyheaders["host"] = true;         response.cache.varybyparams["myparam"] = true;     } 

but if put datetime.now in 1 of pages changes on every request. doesn't seems work.

i have tried put on application_prerequesthandlerexecute event in this answer no luck.

is there way achieve behavior global.asax?

note: want filter url's being cached.

edit: started bounty

steven v's answer put me on right track, after hours of developing "my own cache system" extending actionfilterattribute, had lot of problems threads, response types , cache mashing results it's complete disaster (i think it's not worth pasting code here).

another option control directly iis, if there cache module iis can me achieve or configuration can made on iis's output caching helpful. in end try achieve url related.

the main idea have global cache enabled filtering, url depending, controlled centralized area.

i think can achieve output caching adding outputcacheattribute filters on application start.

there's registerglobalfilters(globalfilters.filters); in application_start. inside registerglobalfilters add:

filters.add(new outputcacheattribute { duration = 300, varybyheader = "host", varybyparam = "myparam" }); 

obviously have unintended side effects since rendered cached 300 seconds. if don't want have on every single action, i'd [outputcache] attribute, , decorate controllers want cached.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -