Take开发者_如何学JAVA the following lines of code that would work fine in a c# asp.net file:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
Response.Cache.SetCacheability(HttpCacheability.Public);
Can I set these in some way in global.asax so that by default my entire application will support 300 second public caching?
Yes you can do that, probably on
protected void Application_PreRequestHandlerExecute(HttpApplication sender, EventArgs e)
{
string sExtentionOfThisFile = System.IO.Path.GetExtension(HttpContext.Current.Request.Path);
if (sExtentionOfThisFile.EndsWith("aspx", true, System.Globalization.CultureInfo.InvariantCulture) ||
sExtentionOfThisFile.EndsWith("ashx", true, System.Globalization.CultureInfo.InvariantCulture) ||
sExtentionOfThisFile.EndsWith("xml", true, System.Globalization.CultureInfo.InvariantCulture)
)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
Response.Cache.SetCacheability(HttpCacheability.Public);
}
}
but then you can not removed so easy... if any pages need to.
(I have include a page test, but just to see it, you can chose and test, if you won everything to have this header.)
精彩评论