【问题标题】:is it possible to use asp.net page level caching with vary by param and localization是否可以使用 asp.net 页面级缓存,并因参数和本地化而异
【发布时间】:2011-07-26 04:20:42
【问题描述】:

我想在我的 asp.net 页面上使用页面级缓存。因参数而异工作正常 - 但是,页面内容以原始请求的语言缓存。如果请求相同的 url / 参数并且用户在与第一个请求不同的语言环境中查看(例如法语而不是英语) - 缓存以原始请求的语言(英语)返回页面内容。有没有办法根据参数变化和基页面类中的值(如从 Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName 返回的属性值)来缓存页面?

【问题讨论】:

    标签: asp.net caching


    【解决方案1】:

    在 OutputCache 中设置 VaryByCustom="Language" 并更新 Global.asax 文件以通过添加以下代码覆盖 HttpApplication.GetVaryByCustomString 方法:

    public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom.Equals("Language"))
            {
                return System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
            }
            else
            {
                return base.GetVaryByCustomString(context, custom);
            }
        }
    

    此代码将使缓存所依赖的参数是您的页面文化。

    【讨论】:

    • 我使用了这个代码,第一次改变语言。但是,不要切换回第一语言..
    • 此代码用于根据使用的语言进行缓存,而不是用于更改语言,因此该函数所依赖的语言切换逻辑可能存在问题。
    • 您如何帮助我解决我的问题:stackoverflow.com/questions/12653343/…
    【解决方案2】:

    使用数据缓存,您可以指定缓存名称。当我遇到这个问题时我做了什么我用页面名+本地化作为缓存名缓存页面数据

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 2011-07-08
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多