我可以将我的缓存行添加到global.asax吗?

以下代码行在ac#asp.net文件中可以正常工作:

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

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

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

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

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

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