ASP.Net Outputcache 以编程方式无法在部分视图中工作

发布于 2024-09-06 14:10:59 字数 396 浏览 2 评论 0原文

以下代码在部分视图中不起作用。

<%
  // Pages always expire at midnight.
  Response.Cache.SetExpires(DateTime.Today.AddHours(24));

  if (variable > 0)
  {
      Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
  }
  else
  {
      Response.Cache.SetCacheability(HttpCacheability.Public);
  }
%>

谁能告诉我为什么?部分视图永远不会被缓存,并且没有明显的原因。我应该编写代码来激活缓存吗?如何?

The following code is not working in a partial view.

<%
  // Pages always expire at midnight.
  Response.Cache.SetExpires(DateTime.Today.AddHours(24));

  if (variable > 0)
  {
      Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
  }
  else
  {
      Response.Cache.SetCacheability(HttpCacheability.Public);
  }
%>

Can anyone tell me why? The partial view is never cached and there is no apparent reason. Should I write code to activate the cache? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-09-13 14:10:59

尝试将代码放入控件的代码隐藏文件 Page_Load 或 Page_Init 事件中。我怀疑因为您的代码内联在 ascx 文件中,所以它在控件的生命周期中执行得太晚了。

Try putting the code into the control's codebehind file Page_Load or Page_Init event. I suspect that because your code is inline in your ascx file, it is being executed too late in the lifecycle of the control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文