zoukankan      html  css  js  c++  java
  • 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面

    为每个session页配制缓存是通过 @ OutputCache 指令的属性 VaryByCustom 来实现的。按照下面的脚本(包含到一个.aspx文件中),花上10秒钟就可以为各个session ID用户配制独立的页面版本。

    <%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="SessionID" %>

    要用到 VaryByCustom="SessionID" ,就要在在服务器的根目录下有Global.asax文件,在文件中声明以下用法:

    <script language="C#" runat="server">
      public override string GetVaryByCustomString(HttpContext context, string arg)
      {
          if (arg.ToLower () == "sessionid") {
              HttpCookie cookie =
               context.Request.Cookies["ASP.NET_SessionId"];
               if (cookie != null)
                  return cookie.Value;
          }
          return base.GetVaryByCustomString (context, arg);
      }
    </script>

    GetVaryByCustomString 是ASP.NET页面输出缓存的基础。它继承于HttpApplication,返回从session cookie得到的session ID。用户在调用GetVaryByCustomString时会被关联到一个session。

    这个技术的缺点是不能用于不支持cookie的用户。另外,用户只有在第二次发出申请时才能获得缓存,因为第一次请求没有合法的session cookie。

  • 相关阅读:
    大一励志的我,现在已经大三了
    Jenkins+K8s实现持续集成
    Jenkins搭建自动化测试环境
    软件开发式样书 6
    软件开发式样书 5
    软件开发式样书 4
    软件开发式样书 3
    软件开发式样书 2
    软件开发式样书 1
    Git学习笔记
  • 原文地址:https://www.cnblogs.com/xiaotaoliang/p/264727.html
Copyright © 2011-2022 走看看