【问题标题】:Can I add my caching lines to global.asax?我可以将缓存行添加到 global.asax 吗?
【发布时间】:2010-03-18 19:26:52
【问题描述】:

采用以下在 c# asp.net 文件中可以正常工作的代码行:

Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
Response.Cache.SetCacheability(HttpCacheability.Public);

我可以在 global.asax 中以某种方式设置这些,以便默认情况下我的整个应用程序将支持 300 秒的公共缓存吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    是的,你可以这样做,可能在

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

    但是你不能那么容易删除......如果有任何页面需要。

    (我已经包含了一个页面测试,但只是为了查看它,你可以选择并测试,如果你赢得了拥有这个标题的一切。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2014-02-07
      • 2018-07-23
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多