【问题标题】:Caching issue: ascx/usercontrol's programmatic Page.Title ignored during cached load of page缓存问题:ascx/usercontrol 的程序化 Page.Title 在页面缓存加载期间被忽略
【发布时间】:2014-03-10 01:44:10
【问题描述】:

问题是:当用户访问 Default.aspx 时,页面加载了 Blog.ascx 内容的缓存版本(因为它在 600 秒内再次访问了同一页面),因此没有执行 Page.Title 代码标题保持为空,而不是像第一次新加载页面时那样有<title>Title of Blog Post</title>


我的 asp.net 网站有 Blog.ascx 来处理加载单个博客文章的内容。 Default.aspx 包含对 Blog.ascx 的引用并使用 Blog.ascx 页面具有自定义缓存:

    <%@ OutputCache Duration="600" VaryByParam="None" VaryByCustom="Url" %>

位于 global.asax.cs 的自定义缓存是:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        switch (custom.ToUpper())
        {
            case "URL":
                return context.Request.Url.ToString().ToLower().Trim();
            default:
                throw new NotImplementedException();
        }
    }

Blog.ascx.cs Page_Load 事件处理程序化标签的值/内容

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Title of Blog Post";
    }

有什么建议吗?

【问题讨论】:

  • 你无法避免这个问题 - 你需要重新设计你的想法以使其发挥作用,要么删除缓存,要么将标题保存在主页上,要么对渲染进行自定义缓存。

标签: c# asp.net caching ascx


【解决方案1】:

您是否考虑过使用用户控件? 如果您为页面内容创建了 UserControl,则可以将 OutputCache 声明移动到该控件,从而释放 Page_Load 事件以在每次访问时设置页面标题。

【讨论】:

  • 如果我正确理解您的评论,您是在暗示我已经在做同样的事情吗?
  • 我完全错过了。对不起。一个选项是使用由 UserControl 设置的公共变量,然后由一些 javascript 引用:document.title = '';
  • 不幸的是,这也不是一个选项,因为搜索引擎/爬虫仍然会看到一个空标题,Javascript 只会将显示解析给最终用户
  • 页眉中的控件,功能一样吗?然后从服务器端代码输出 PageTitle。
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多