1

I Need To Set Expiry Headers In my Asp.net code.. Is there any way through which I Can Add expiry Headers through code.?

I Have tried adding the following code in my asp page

<% System.Web.HttpContext.Current.Response.AddHeader( "Cache-Control","no-cache"); System.Web.HttpContext.Current.Response.Expires = 0; System.Web.HttpContext.Current.Response.Cache.SetNoStore(); System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache");%>

<%@ OutputCache Duration="86400" Location="Client" VaryByParam="None" %>

and added the following in my c# page...

Response.AddHeader("Expires", "Thu, 01 Dec 2014 16:00:00 GMT");
AND

Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetMaxAge(TimeSpan.FromSeconds(3600)); Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(3600));

and added this to web,config file

<clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />

Crazy Programmer
  • 196
  • 1
  • 3
  • 18

2 Answers2

0
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetExpires(yourCalculatedDateTime);
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
Brijesh
  • 352
  • 5
  • 17
0

you can define it per page like below

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

or you can do it in declaritive way by doing it in your aspx page like below

<%@ OutputCache Duration="60" VaryByParam="None" %>
Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29
  • Is'nt the OutputCache directive just caching the page on the server? IMHO the OP talks about enabling client-side caching with response headers, which is different. – Marcel Feb 11 '14 at 08:36
  • 1
    @marcel acually OutputCache is tuneable base on the location value and if you didnt define location as per microsoft "the page output can be cached on all cache-capable network devices that are involved in the response. These include the requesting client, the origin server, and any proxy servers through which the response passes." – Saddam Abu Ghaida Feb 11 '14 at 08:45
  • Nope Not Helping.. :( – Crazy Programmer Feb 11 '14 at 09:02