开发者

Can I add my caching lines to global.asax?

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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜