本文介绍了我能否添加缓存行的Global.asax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以code以下行,将工作细在C#asp.net文件:

Take 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);

我可以在Global.asax中设置这些以某种方式使默认我的整个应用程序将支持物300秒的公共缓存?

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

这篇关于我能否添加缓存行的Global.asax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:39